TextInput React National无法使用OnChangeText
该文本不会在屏幕{account}上更新。当您在文本输入上键入时,似乎没有调用OnchangeAccount。我已经破坏了它,但是除非将文本放入应用返回语句中,否则它行不通。它键入一项工作,然后将键盘删除。
...
const TextInputScreen = () => {
const [account, onChangeAccount] = React.useState(null);
const ExternalTextInputContainer =({account,onChangeAccount})=>{
return(
<TextInput
style={styles.input}
onChangeText={onChangeAccount}
value={account}
/>
)
}
return (
<SafeAreaView>
/>
<Text>{account}</Text>
<ExternalTextInputContainer value={account} onChangeText={
onChangeAccount} />
</SafeAreaView>
);
};
const styles = StyleSheet.create({
input: {
height: 40,
margin: 12,
borderWidth: 1,
padding: 10,
},
});
export default TextInputScreen;
The text does not update on the screen {account}. When you type on the text input it seems like the onChangeAccount is not called. I have destructured it but it does not work unless I put the text inside the app return statement. It types one work then dismisses the keyboard.
...
const TextInputScreen = () => {
const [account, onChangeAccount] = React.useState(null);
const ExternalTextInputContainer =({account,onChangeAccount})=>{
return(
<TextInput
style={styles.input}
onChangeText={onChangeAccount}
value={account}
/>
)
}
return (
<SafeAreaView>
/>
<Text>{account}</Text>
<ExternalTextInputContainer value={account} onChangeText={
onChangeAccount} />
</SafeAreaView>
);
};
const styles = StyleSheet.create({
input: {
height: 40,
margin: 12,
borderWidth: 1,
padding: 10,
},
});
export default TextInputScreen;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要将价值传递给Onchangeaccount。您不会传递任何东西。
更新:
您无需像现在这样做的那样将道具传递给外部textinputContainer组件。另外,您正在尝试从功能中的道具中解构帐户和Onchangeaccount。除了您没有将这些属性传递给您的功能外,它可能会引起冲突,因为您已经定义了此类函数/变量名称。您可以从外部textinputContainer常数函数中删除这些道具。
此外,关于驳回键盘问题:
之所以发生这种情况,是因为您将常数函数用作主组件内部的组件。每次输入字符时,这会导致重新启动组件,然后键盘被删除。您可以在textInputscreen之外定义组件,然后将其导入。或使用externalTextinputContainer作为函数。因此,您需要这样称呼它:
有关更多信息,您可以阅读此信息: stackoverflow.com/a/a/60048240/525747777
You need to pass the value to onChangeAccount. You're not passing anything to it.
Update:
You don't need to pass props to ExternalTextInputContainer component like you're doing now. Also, you're trying to deconstruct account and onChangeAccount from the props in the function. Apart from you don't pass such properties to your function, it can cause conflicts since you already have such functions/variable names defined. You can remove those props from your ExternalTextInputContainer constant function.
Also, about dismissing the keyboard issue:
It happens because you used the constant function as a component inside your main component. This causes rerendering the component each time you type a character and then your keyboard gets dismissed. You can either define the component outside of TextInputScreen and then import it. Or use ExternalTextInputContainer as a function. So, you'll need to call it like this:
For more info, you can read this: stackoverflow.com/a/60048240/5257477
它是因为您将任何道具传递给外部textinputContainer组件。
更改此:
Its because you arent passing any props to the ExternalTextInputContainer component.
Change to this:
只是改变一点:
just change a little:
我在上面得到了帮助,问题是由于文本输入是一个组件而引起的,并且每次都会重新呈现,每次引起键入并在键入后忽略键盘。解决方案是将组件转换为函数或从组件文件夹导入组件。
I got assistance above, the problem was caused by the fact the textInput is a component and it re-renders every time causing it to flicker and dismiss keyboard after typing. The solution would be to convert the component into a function or import the component from your components folder.