I am using material-ui
Here my react component goes:
import Checkbox from '@material-ui/core/Checkbox'; import FormControlLabel from '@material-ui/core/FormControlLabel'; import React from "react"; import { withStyles } from "@material-ui/core/styles"; const styles = (theme) => ({ }); class SomeComponent extends React.Component { static propTypes = { }; state = { checked: true, } handleChange = name => event => { event.persist() this.setState({ [name]: event.target.checked }, () => { if (event.target.checked) { this.props.parentMethod1(event.target.value) } else { this.props.parentMethod2(event.target.value) } }); }; render() { const { user } = this.props; return ( <div> <FormControlLabel control={ <Checkbox checked={this.state.checked} onChange={this.handleChange('checked')} value={user.id} color="primary" /> } label={user.first_name} /> </div> ); } } export default withStyles(styles)(SomeComponent); The problem is, I can select/unselect the checkBox only once. After selecting/deselecting the checkbox, onChange event is not occuring.
Can you help me how to make Checkbox work as the way it is? Thanks.
Here is the reproduced error:
event.target.checkedandevent.target.valuein the callback, you can putcheckedandvaluein variables the first thing you do in the function returned fromhandleChange:const { checked, value } = event.targetand use that instead. This way you don't need to persists the event. Does that fix the issue?