I'm trying to implement some functionality that will allow for the name of a checkbox to be pushed to an array in the components state and also to have that name removed when the checkbox is unchecked, I can push to the array just fine, it's removing items from it that I'm having trouble with.
I've tried using splice but the issue I'm having is it seems that the array is lagging one action behind, thus not allowing me to splice out the correct item.
handleCheckboxChange = event => {
if (event.target.checked) {
this.setState({
check: [...this.state.check, event.target.name]
});
} else if (!event.target.checked) {
let index = this.state.check.indexOf(event.target.name);
this.setState({
check: this.state.check.splice(index, 1)
});
}
};
Expected result is that items will be added and removed from the array as the user checks or unchecks the corresponding checkbox.