我有这个代码:在reactjs中 http://codepen.io/fernandooj/pen/MJOodr
1- list an array, when mapping I send each item a checkbox
2- I have another array that I still do not have
3- I need to compare the two arrays and if there is an element of both arrays
等于列表中重复的复选框处于活动状态 例 Array1 [1,2,3] Array2 [2] 复选框" 2"看似cheked
答案 0 :(得分:1)
您可以call a function with item
作为参数,因此defaultChecked
值。确保你不绑定函数,因为defaultChecked
期望值而不是函数。
getChecked(item) {
console.log(item);
if(array2.indexOf(item) > -1) {return true}
return false;
}
<input
key={item}
name='list'
type='checkbox'
onClick={(e)=>this.updateStateList(e,item)}
label='Add'
className='listTour'
defaultChecked={this.getChecked(item)}/>
<强> CodePen 强>