【问题标题】:How can I make a textField in react native required我怎样才能在需要反应本机时制作一个文本字段
【发布时间】:2019-01-10 21:45:34
【问题描述】:

我有一个文本字段和一个提交按钮,但是当我单击提交按钮并且没有在 Textfield 内写入任何内容时,它仍然会继续该过程,所以我想让我的文本字段成为必需的文本字段,但不要不知道怎么做?

【问题讨论】:

    标签: forms react-native textfield required


    【解决方案1】:

    这是带有演示 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>
            )}
    

    【讨论】:

    • @ArnavNath 有帮助吗?然后接受答案。这对于检查类似问题的人会有所帮助
    猜你喜欢
    • 1970-01-01
    • 2018-07-02
    • 2022-11-19
    • 2014-04-14
    • 1970-01-01
    • 2019-02-09
    • 2023-02-09
    • 1970-01-01
    • 2014-08-23
    相关资源
    最近更新 更多