React checkbox - Onchange ouput as undefined for 1st click(time). After that showing valid true or false output.I googled this issue but not solving the problem.
Please help, What i'm doing wrong here.
I defined checked as false in the State:
constructor() {
super();
this.state = {
formFields: {
checked: false,
},
};
}
Handling the checkbox:
handleCheckBox = (e) => {
this.setState({ checked: !this.state.checked });
console.log("checked", this.state.checked);
// this.setState({ checked: !this.state.formFields.checked });
// console.log("checked", this.state.formFields.checked);
};
destructuring:
const { ...formFields } = this.state;
In the form checkbox input as follows:
<input
type="checkbox"
checked={formFields.checked}
onChange={this.handleCheckBox}
/>
