skip to Main Content

I have a basic form that includes two text inputs and one button using a project created from the react native CLI.

const Signup = () => {
  const [username, setUsername] = useState('')
  const [password, setPassword] = useState('')

  const handleChangeUsername = () => {
    setUsername(username)
  }
  const handleChangePassword = () => {
    setPassword(password)
  }

  const btnSignup = () => {
    // auth()
    // .createUserWithEmailAndPassword(setUsername,setPassword)
    // .then(() => {
    //   console.log('User account created & signed in!');
    // })
  }

  return (
    <View>
      <View>
        <TextInput onChangeText={handleChangeUsername}> Kullanıcı Adı </TextInput>{' '}
        <TextInput onChangeText={handleChangePassword}> Password </TextInput>{' '}
      </View>{' '}
      <View>
        {' '}
        <Button onPress={btnSignup} title="TIKLA" />{' '}
      </View>
    </View>
  )
}

export default Signup

I can’t use the btnSignup method because when it’s run give an error. What is the issue here?

2

Answers


  1. Id replace your text inputs as the below

    <View>
    <TextInput onChangeText={setUsername} value={username} placeholder="Username" />
    <TextInput onChangeText={setPassword} value={password} placeholder="Password" secureTextEntry={true}/>
    </View>
    

    I then get no errors when clicking button. If youre trying to console log the username write console.log(username) instead of console.log(setUsername)

    Login or Signup to reply.
    1. You are trying to console.log(setUsername) which is a state setter
      function. If you want to log the value you need to write:
      console.log(username)
    2. and also you need to update your handleChangeUsername and
      handleChangePassword to receive input value and update state which
      would look like this: const handleChangeUsername = (value) => { setUsername(value); }

    and if you want to log username nad password you can do it inside btnSignup which would look like this:

    const btnSignup = () => {
      console.log('Username:', username);
      console.log('Password:', password);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search