react-native - 如何从 TextInput 获取值

标签 react-native textinput react-native-textinput

我陷入了一个看似简单的函数中。 如何从 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/

相关文章:

javascript - 为什么我得到 "undefined is not an object (evaluating PropTypes.shape)"?

android - react-native map 没有为 android 加载(空白 map )

html - HTML 中的文本输入框/下拉菜单

javascript - React native - 为 TextInput 添加货币前缀

React-native - TextInput 小数点在 iOS 上显示逗号而不是点

ios - 覆盖/禁用 TextInput 中的 iOS 智能标点符号

android - 如何在 iPhone 上运行 Ubuntu 操作系统的 React Native 应用程序

android - 为什么 React Native Picker 在我的 Android 模拟器中无法工作?

java - 将用户输入的单词的每个字符放入java中单独的char变量中