反应原生中的 event.target.value [英] event.target.value in react-native
问题描述
我在 react-native 中有一个 input 元素,它的 onChange
绑定到 handleClientInput
.我正在将动态数据分配给一个状态,它在网络上运行良好,但在 android 设备上 event.target.value
返回未定义:
I have an input element in react-native, which its onChange
is binded to handleClientInput
. I'm assigning the dynamic data to a state, It works perfectly on the web, but on an android device event.target.value
is returning undefined:
const handleClientIDInput = (event) => {
console.log("Input change");
console.log(event.target.value); // logs undefined
setClientID(event.target.value);
};
这是我对功能组件的回报:
This is my return for my functional component:
return (
<SafeAreaView>
<TextInput
style={{
borderWidth: 1,
borderColor: "black",
}}
onChange={handleClientIDInput}
/>
<Button title="Proceed" onPress={handleButtonClick} />
</SafeAreaView>
);
我使用的是 React Native 而不是 ReactJS,因为这在 ReactJS 上运行正常
I am using React Native and not ReactJS, since this works okay on ReactJS
推荐答案
在 iOS 和 Android 中,您使用 React Native 和 ReactJS.这意味着您必须稍微调整 TextInput 组件,因为您无法从 event.target.value 访问文本.看看这个例子,这就是你可以让它工作的方法.
In iOS and Android you are using React Native with ReactJS. So that means that you have to tweak your TextInput component a little bit since you can't access text from event.target.value. Look at the example, this is how you can make it work.
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
onChangeText={text => console.log(text);
value={value}
/>
当您为 iOS 或 Android 进行开发时,我建议您同时查看 React Native 的文档,而不仅仅是 React 的普通文档.
I recommend looking also to the docs from React Native when you are developing for iOS or Android and not only the react plain docs.
这篇关于反应原生中的 event.target.value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!