简体   繁体   English

如何在react-native中处理复选框动作

[英]how to handle checkbox action in react-native

I have to call POST TYPE node Js API Function on button click but only when ever the user checks the checkbox. 我必须在按钮单击时调用POST TYPE节点Js API Function,但仅当用户选中该复选框时才可以。 I am new to react-native.I am wondering how to handle checkbox action? 我是本机新手,我想知道如何处理复选框动作?

you can use states to determinate whether or not the checkbox is checked: 您可以使用状态来确定该复选框是否已选中:

On your constructor set the state: 在构造函数上设置状态:

constructor(props) {
  super(props);
  this.state = {
    checked: false
  };
}

Your checkbox: 您的复选框:

<View>
   <CheckBox
      value={this.state.checked}
      onValueChange={() => 
        this.setState({checked:!this.state.checked})
      }
   />
</View>
<View>
  <Button title="Validate" onPress={this.validateHandler} />
</View>

Now your handler where if the checkbox pass he validation you can call your API method: 现在是您的处理程序,如果复选框通过验证,您可以在其中调用API方法:

validateHandler = () => {
   if (this.state.checked) {
     console.log("validated"); //call the api
   }else{
     console.log("not checked"); //return and show a message or something
   }    
};

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

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