React Native TextInput not updating state in a functional component using onhange

Viewed 579

I am using a in a react native functional component and onChange the state does not seem to be updating for "name".

I am console logging the new state using useEffect to check whether or not the state is updated and it is returning undefined as if the input text was never set as the state "name"

function AddTask ()  {
    
    const [name, setName] = useState('');
  
    useEffect(() => {
        console.log(name),name;
        
    })

    const updateTaskInfo = event => setName(event.target.value);
    
        return (
             <TextInput
                  onChange={updateTaskInfo}
                  placeholder="Name"
                  value={name}
             />
 );
}

export default AddTask;
1 Answers

use onChange like that event.nativeEvent.target.value

<TextInput onChange={ event => setName(event.nativeEvent.target.value) } />

or use onChangeText like that

<TextInput onChangeText ={ value => setName(value ) } />
Related