我使用了Material UI的复选框组件,并尝试通过onCheck来切换状态,在控制台中状态发生了改变,但在UI中没有变化,勾选标记没有切换。我做错了什么。
class CheckboxInteractivity extends React.Component {
state = {
switched: false,
}
componentWillMount() {
const {checked} = this.props
if (checked) {
this.setState({
switched: true,
})
}
}
handleChange = (event, switched) => {
this.setState({switched: !this.state.switched})
}
render () {
const {switched} = this.state
return <Checkbox
label="Label"
checked={switched}
onCheck={this.handleChange}
{...this.props}
/>
}
}
CheckboxInteractivity.propTypes = {
checked: PropTypes.bool,
}
export default CheckboxInteractivity
组件
<CheckboxInteractivity />
//working correctly
<CheckboxInteractivity checked/>
//not working