繁体   English   中英

如何在 react-native 中获取 TextInput 值

[英]How to get the TextInput value in react-native

我有这个代码,我在那里得到用户输入。 但是当我在控制台记录它以检查它时,我得到了一个object of type undefinedobject of type undefined 我在这里提供我的代码。 请更正我的代码,以便修复此错误。

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

你必须使用 onChangeText 道具

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

这会将当前文本传递给goalInputHandler 函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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