简体   繁体   中英

How to get the TextInput value in react-native

I have this code where I'm getting a user input. But when I'm console logging it to check it I'm getting an object of type undefined . I'm providing my code here. Please correct my code so that this bug is fixed.

 export default function App() { const [enteredGoal, setGoal] = useState(''); const goalInputHandler = (enteredText) => { setGoal(enteredText); }; const addGoalHandler = () => { console.log(enteredGoal); } return ( <View style={styles.screen}> <StatusBar style="auto" /> <View style={styles.inputTextContainer}> <TextInput style={styles.TextInput} placeholder="Course Goal" onChange={goalInputHandler} value={enteredGoal} /> <Button title="ADD" onPress={addGoalHandler} /> </View> </View> ); }

You have to use the onChangeText prop

onChangeText={(text)=>goalInputHandler(text)}

This will pass the current text to goalInputHandler function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM