I have a simple react component:
export default class RidiculousCheckbox extends Component {
constructor(props) {
super(props);
this.state = {
isChecked: true,
};
}
handleInputChange(event) {
window.console.log("Value of event:" + event.target.checked);
this.setState({isChecked: event.target.checked});
window.console.log("State after setting, is checked:" + this.state.isChecked);
}
render() {
return (
<form>
<input
type="checkbox"
checked={this.state.isChecked}
onChange={this.handleInputChange.bind(this)}/>
</form>
);
}
}
When I display it for the first time the box is checked. Then I click on it, the box unchecks
Value of event:false
State after setting:true
If i click it again, the check box comes back, and the output:
Value of event:true
State after setting:false
Why is the state being set to true
in the first example, when the value of event.target.checked
is clearly false
?