Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React Native simple useState does not work, state is staying 'undefined' and "setState is not a function"

I have just a simple React-Native Component that uses useState as a test if it works. When I set the userstate, and I just print it, it logs ‘undefined’. Also when I call setUser, it throws an error, that setUser is not a function. Whats wrong with my Setup?:

import { StyleSheet, Text, View, TextInput, Button, Seperator, Pressable } from 'react-native'
import React, {useState, useEffect} from 'react'
import colors from '../theme/colors.style.js'
import NetInfo from '@react-native-community/netinfo';
import { checkConnected } from '../utils/Netinfo.js';

export default function  MachineManager() {
  const {connectStatus, setConnectStatus} = useState(false);
  const {user, setUser} = useState("martin");
  console.log(user);

  return (
    <View style={styles.container}>
      <View style={styles.connectorMenu}>
        <View style={styles.shadowView}>
          <View style= {styles.connectionMenuHeader}>
            <Text style={styles.connectionMenuHeaderText}>Verbundenes Gerät</Text>
          </View>
          <View style={styles.inputRow}>
            <Text style={styles.inputLabel}>SSID:</Text>
            <TextInput placeholder="SSID Kennung" style={styles.input}/>
          </View>
          <View style={styles.inputRow}>
            <Text style={styles.inputLabel}>Passwort:</Text>
            <TextInput placeholder="Passwort" style={styles.input}/>
          </View>
          <View style={styles.connectionControl}>
            <View style={styles.connectionControlStatus}>
              <Text style={styles.connectionControlStatusText}>Status: Nicht Verbunden</Text>
              <Text style={styles.connectionControlStatusText}>{user}</Text>
            </View>
            <View style={styles.connectionControlConnect}>
              <Pressable style={styles.connectButton}>
                <Text style={styles.connectButtonText}>Verbinden</Text>
              </Pressable>
            </View>
          </View>
        </View>
      </View>
    </View>
  )
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The syntax is incorrect. It needs to be –

const [connectStatus, setConnectStatus] = useState(false);
const [user, setUser] = useState("martin")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading