Home > Blockchain >  React Native simple useState does not work, state is staying 'undefined' and "setStat
React Native simple useState does not work, state is staying 'undefined' and "setStat

Time:07-11

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>
  )
}

CodePudding user response:

The syntax is incorrect. It needs to be -

const [connectStatus, setConnectStatus] = useState(false);
const [user, setUser] = useState("martin")
  • Related