reactjs - 如何处理循环中的复选框反应原生
问题描述
如何在本机反应中处理循环内的复选框。目前,我的代码是这样的。
this.state.allItems.map((res, index) => {
return(
<CheckBox
color="green"
style={{ marginRight: 20, }}
checked={this.state.check1}
onChange={()=>{ }}
/>
)
})
解决方案
如果我正确理解您的问题,则您有多个项目,并且您希望它们可以独立检查。
为此,您需要让它们每个都有自己的状态值。你如何做到这一点取决于你的设计需求,但形成你的代码你需要一个数组而不是 check1 状态。
像这样的东西:
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});
}
}
/>
当然,您需要使用数组正确填充检查状态。
推荐阅读
- android - android菜单项不会出现在顶栏上
- javascript - 停止输入 React Js 时调用函数
- python - 空时如何让 QIntValidator() 调用editingFinished?
- python - For循环获取素数
- sql - SQL将值的总和分组为范围
- azure - 取消 Azure DevOps YML Pipelines 中的活动作业
- apache-tika - 如何使用 Apache Tika Server 从 PDF 中提取内联图像并将其保存为文件?
- powerbi - 如何在 Power Query 中添加重复的索引或排名列(1、1、2、2、3、3、4、4 等)
- python - 如何避免芹菜截断“django_celery_results_taskresult”中的“task_args”文本
- javascript - JavaScript window.open,将脚本传递给子窗口只能工作一次