I'd like to know how to toggle a boolean state of a React component. For instance:
I have a boolean state check in the constructor of my component:
constructor(props, context) {
super(props, context);
this.state = {
check: false
};
};
I am trying to toggle the state each time my checkbox is clicked, using the this.setState method:
<label>
<input
type=checkbox"
value="check"
onChange={(e) => this.setState({check: !check.value})}
/>
Checkbox
</label>
Of course I get a Uncaught ReferenceError: check is not defined. So how can I achieve this?
this.state.checkinthis.setState({check: !check.value}). And add the property checked for checkbox, which would change according to component state.checked={this.state.checked}