项目场景:
<input type="checkbox" checked={doneCount===total? true:false}/>
问题描述
react 报错
react-dom.development.js:86 Warning: You provided a `checked` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultChecked`. Otherwise, set either `onChange` or `readOnly`.
原因分析:
checked值无法改变,需要搭配onChange
解决方案:
<input type="checkbox" defaultChecked={doneCount===total? true:false}/>
将checked改为defaultChecked
注意:但是defaultChecked只能第一次时奏效,所以后面是否选中修改时不起作用,给input添加一个onChange
<input type="checkbox" onChange={this.handleCheckAll} checked={doneCount===total? true:false}/>
//全选checkbox的回调
handleCheckAll=()=>{
this.props.checkAllTodo()
}