2
我使用Material UI复选框组件,并尝试在控制台状态更改中切换状态onCheck,但是在UI中,复选标记不会切换。我弄乱了什么。复选框状态不切换。 Material UI React
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
中,如果我想尝试添加禁用的道具,我需要{...} this.props,因为取决于该道具我设置样式情况下,只有一个问题 –
@PalaniichukDmytro检查更新的答案,更新部分。你只需要先应用'props'值然后再应用其他值,排序将解决你的问题:) –