简体   繁体   English

如何处理循环中的复选框反应原生

[英]How to handle checkbox in loop react native

How do I handle checkbox inside of loop in react native.如何在本机反应中处理循环内的复选框。 Currently, my code is like this.目前,我的代码是这样的。

this.state.allItems.map((res, index) => {
 return(
     <CheckBox 
         color="green"
         style={{ marginRight: 20, }}
         checked={this.state.check1}
         onChange={()=>{ }}
/>

)

})

If I understand your question correctly, you have multiple items and you want them to be independently checkable.如果我正确理解您的问题,则您有多个项目,并且您希望它们可以独立检查。

To do that you will need for them to each have their own state value.要做到这一点,您需要让他们每个人都有自己的 state 值。 How you do this will depend on your design needs, but form your code you would need to have an array instead of the check1 state.如何执行此操作将取决于您的设计需求,但在您的代码中,您需要一个数组而不是 check1 state。

Something like this:像这样的东西:

this.state.allItems.map((res, index) => {
 return(
     <CheckBox 
         color="green"
         style={{ marginRight: 20, }}
         checked={this.state.checked[index]}
         onChange={()=> {
                  let { checked } = this.state;
                  checked[index] = !checked[index];
                  this.setState({checked});
         }
 }
/>

Of course you will need to populate the checked state properly with an array.当然,您需要使用数组正确填充已检查的 state。

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

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