我陷入了一个看似简单的函数中。 如何从 TextInput 获取值(字符串)?
这里是代码摘录:
const Insert = props => {
const [enteredName, setEnteredName] = useState();
const sendValues = (enteredName) => {
console.log(enteredName);
};
<TextInput
placeholder="Your Name"
blurOnSubmit
autoCorrect={false}
maxLength={30}
autoCapitalized="words"
placeholderTextColor="#777"
value={enteredName}
onChangeText={text => setEnteredSurname(text)}
/>
<View>
<Button title="Submit" onPress={sendValues(enteredName)} />
我在打字时收到了打字信息,但它没有提交任何内容。
有什么想法吗?
提前致谢!
最佳答案
您应该将 onPress 从表达式转换为函数并初始化您的状态
const Insert = props => {
const [enteredName, setEnteredName] = useState(''); //INIT TO EMPTY
function sendValues(enteredName) {
console.log(enteredName);
};
<TextInput
placeholder="Your Name"
blurOnSubmit
autoCorrect={false}
maxLength={30}
autoCapitalized="words"
placeholderTextColor="#777"
value={enteredName}
onChangeText={text => setEnteredSurname(text)} />
<View>
<Button title="Submit" onPress={() => sendValues(enteredName)} /> //Function not expression
</View>
关于react-native - 如何从 TextInput 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60091873/