Answer the question
In order to leave comments, you need to log in
How to properly overwrite state value?
Hello,
here is my state:
this.state = {
...
checkboxes: {
a: true,
b: true
}
};
...
this.setState({
checkboxes[e.target.value]= !state
});
Answer the question
In order to leave comments, you need to log in
You need to get your object, change the field and send back in setState
1) via object cloning
let checkboxes= Object.assign({}, this.state.checkboxes); // Создаем копию, можно просто ссылку взять, но копию безопаснее
checkboxes[e.target.value] = false; // изменяем поле
this.setState({checkboxes}); // Сохраняем
this.setState({
checkboxes: {
...this.state.checkboxes,
[e.target.value]: false // измененное поле объекта checkboxes
}
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question