score:2

Accepted answer

I believe it's better not to touch the original state of the object. Instead copying and modifying in that new object the property what we need then passing it to setState(). The proper way is using the callback version of setting the state.

Try as the following:

setItems(prevState => ({
   ...prevState,
   item_name: 'the new value'
}));

So the function would look like:

some_method = (new_item_name) => {
   setItems(prevState => ({
      ...prevState,
      item_name: new_item_name
   }));
}

I hope this helps!


Related Query

More Query from same tag