I'd like to know how to toggle a boolean state of a react component. For instance:
I have 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?
Many thanks in advance.
this.state.check
inthis.setState({check: !check.value})
. And add the property checked for checkbox, which would change according to component state.checked={this.state.checked}
– Vincas Stonys