反应本机中的 event.target.value

ehm*_*mlv 10 reactjs react-native

我在react-native中有一个输入元素,它onChange绑定到handleClientInput. 我将动态数据分配给一个状态,它在网络上完美运行,但在 Android 设备上event.target.value返回未定义:

const handleClientIDInput = (event) => {
    console.log("Input change");
    console.log(event.target.value); // logs undefined
    setClientID(event.target.value);
  };
Run Code Online (Sandbox Code Playgroud)

这是我的功能组件的回报:

return (
    <SafeAreaView>
      <TextInput
        style={{
          borderWidth: 1,
          borderColor: "black",
        }}
        onChange={handleClientIDInput}
      />
      <Button title="Proceed" onPress={handleButtonClick} />
    </SafeAreaView>
  );
Run Code Online (Sandbox Code Playgroud)

我使用的是 React Native 而不是 ReactJS,因为这在 ReactJS 上工作正常

use*_*135 9

在 iOS 和 Android 中,您将 React Native 与 ReactJS 结合使用。因此,这意味着您必须稍微调整 TextInput 组件,因为您无法从 event.target.value 访问文本。看一下这个例子,这就是让它工作的方法。

<TextInput
  style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
  onChangeText={text => console.log(text);
  value={value}
/>
Run Code Online (Sandbox Code Playgroud)

我建议您在为 iOS 或 Android 进行开发时也查看 React Native 的文档,而不仅仅是 React 普通文档。