简体   繁体   English

有条件地禁用react-native中的按钮

[英]Conditionally disable buttons in react-native

I have two input fields and a button: 我有两个输入字段和一个按钮:

<Item regular >
    <Input onChangeText={(text) => this.setState({newEmail: text})} value={this.state.newEmail}/>
</Item>

<Item regular >
    <Input onChangeText={(text) => this.setState({confirmEmail: text})} value={this.state.confirmEmail}/>
</Item>

<Button  onPress={() => this.update()} style={styles.updateButton} block> 
    <Text> Send </Text>
</Button>

What I want to do is : 我想做的是:

OnTextChange check that : These two fields are == and they pass reg.text(email) I have defined, if so, enable UpdateButton . OnTextChange检查:这两个字段是==,并且它们通过我定义的reg.text(email) ,如果是,请启用UpdateButton Else keep it disabled. 否则将其禁用。 How do I do such validation in react-native? 如何在react-native中进行此类验证? I tried writing a function for onChangeText in the constructor, but I couldn't get it to work. 我尝试在构造函数中为onChangeText编写函数,但无法使其正常工作。

I had: 我有:

  constructor() {
 super();
this.state = {
newEmail: '',
confirmEmail: '',
};
}

handleEmailChange = (evt) => this.setState({ newEmail: evt.target.value });

<Input onChangeText={this.handleEmailChange} value={this.state.newEmail}/>

Because I tried to handle the input in the handleEmailChange so I could check for both, but after every letter written, the previous would get overwritten. 因为我试图处理handleEmailChange中的输入,所以我可以检查两者,但是在写完每个字母之后,前一个字母将被覆盖。 That's the part I'm unsure about. 那是我不确定的部分。 In Angular I have achieved the same using onChange() and then the ngModel to pass the input values and check there. 在Angular中,我使用onChange()然后通过ngModel传递输入值并在那里进行检查来实现相同的目的。 But here I don't know how to pass the values onChangeText AND check 但是这里我不知道如何传递值onChangeText和检查

在JSX中,您可以像这样检查。
{this.state.newEmail && this.state.newEmail===this.state.confirmEmail &&
<Button onPress={() => this.update()} style={styles.updateButton} block>
<Text> Send </Text>
</Button>

I'm not familiar with react-native but i think it's easy to redo for react-native too. 我对react-native并不熟悉,但是我也很容易重做react-native So my code will based on react : 所以我的代码将基于react

You can use disabled property of <Button> component like below: 您可以使用<Button>组件的disabled属性,如下所示:

...
this.state = {
  newEmail: '',
  confirmEmail: '',
  isDisabled: false
}
...

My render method looks: 我的render方法如下:

<div>
   New Email <input type="text" name="newEmail" onChange={this.handleEmailChange} value={this.state.newEmail}/> 
   <br/>
   Confirm Email <input type="text" name="confirmEmail" onChange={this.handleEmailChange} value={this.state.confirmEmail}/>
   <br/>
   <button disabled={this.state.isDisabled}>Woot</button>
 </div>

And here is handleEmailChange method: 这是handleEmailChange方法:

handleEmailChange(e){
    this.setState({ [e.target.name]: e.target.value }, () => {
        //since we don't know which field we have changed it's good to check in a callback.
        this.setState({isDisabled: this.state.newEmail !== this.state.confirmEmail})
    })
  }

Worked example

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

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