Thanks in advance. I have a state array as below.
I need to add an item to state array, I came across that we need not do state mutation. How do i set state with prevSta
More readable and cleaner solution it would be:
Create a variable that holds a copy of the actual state:
If state is an array and you need to add an element in it
let newState = [...messages, 'Hi buddy'];
setMessages(newState);
If state is an object and you need to update a property in it
let newState = Object.assign({}, message, {name: 'Michael Scott'});
setMessages(newState);