React·100 questions

How to update objects and arrays in state without mutations?

Answer
Do not mutate state directly.
Object: setS(prev => ({ ...prev, a: 1 })).
Array: setA(prev => [...prev, item]).
For deletion: filter.
For complex structures, you can use immer.
Was this answer helpful?

More questions in this topic

Related questions from other topics