Answer the question
In order to leave comments, you need to log in
Arrays of objects in state - how to update?
Given:
In the state array, in the array of objects.
Need to:
Update one of the objects, one of the properties.
Smart people taught me not to mutate state and do something like this:
onItemHintClick = (index, e) => {
const newItems = this.state.items.map((item, i) => {
if (i === index) {
return { ...item, htogle: !item.htogle };
} else {
return {...item};
}
});
this.setState({ items: newItems });
};
this.state.items[index].htogle = !this.state.items[index].htogle
this.forceUpdate();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question