简体   繁体   English

按钮-启用和禁用

[英]Button - Enable and Disabled

I made a form using React Native, however I want the record button to be disabled while the TextInput are empty, and when all TextInput is filled in, the button goes back to enabled. 我使用React Native制作了一个表单,但是我希望在TextInput为空时禁用记录按钮,并且当所有TextInput都填充后,该按钮又回到启用状态。

How do I do this? 我该怎么做呢? Can you send me examples? 你能给我发例子吗?

You can do something like this: 您可以执行以下操作:

class Form extends Component {
  constructor(props) {
    super(props);
    this.state = { name: '', email: '' };
  }

  render() {
    const { name, email } = this.state;

    return (
      <View>
        <TextInput
          onChangeText={name => this.setState({ name })}
          value={name}
        />
        <TextInput
          onChangeText={email => this.setState({ email })}
          value={email}
        />
        <TouchableHighlight disabled={!name || !email}>
          Submit
        </TouchableHighlight>
      </View>
    );
  }
}

Basically, you store each value of the TextInput s in the state and you toggle the disabled prop of the Touchable* (also works for Button ) component when all the values are filled. 基本上,您将TextInput的每个值存储在状态中,并在所有值都填满后切换Touchable* (也适用于Button )组件的disabled属性。 And here you can also do some basic validation like length or a matching pattern. 在这里,您还可以进行一些基本的验证,例如长度或匹配模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM