【发布时间】:2019-01-10 21:45:34
【问题描述】:
我有一个文本字段和一个提交按钮,但是当我单击提交按钮并且没有在 Textfield 内写入任何内容时,它仍然会继续该过程,所以我想让我的文本字段成为必需的文本字段,但不要不知道怎么做?
【问题讨论】:
标签: forms react-native textfield required
我有一个文本字段和一个提交按钮,但是当我单击提交按钮并且没有在 Textfield 内写入任何内容时,它仍然会继续该过程,所以我想让我的文本字段成为必需的文本字段,但不要不知道怎么做?
【问题讨论】:
标签: forms react-native textfield required
这是带有演示 https://codesandbox.io/s/lpx76zq6vm 的沙盒链接。当你点击按钮时,你可以触发一个函数来检查你的输入字段是否为空
<Button
onPress={() => {
if (this.state.text.trim() === "") {
this.setState(() => ({ nameError: "First name required." }));
} else {
this.setState(() => ({ nameError: null }));
}
}}
title="Login"
/>
你的输入字段像
<TextInput
style={{ height: 40, borderColor: "gray", borderWidth: 1 }}
onChangeText={text => this.setState({ text })}
value={this.state.text}
/>
{!!this.state.nameError && (
<Text style={{ color: "red" }}>{this.state.nameError}</Text>
)}
【讨论】: