DEV Community

loading...

Discussion on: How to update object or array state in React

Collapse
click2install profile image
click2install • Edited

Or use array methods like concat so you're not increasing your time complexity using iterator semantics.

Your Array example is not updating state as you intended due to poor nomenclature. If you have to spread you can just setState(value => [ ...value, someNewValue ]);, which IMO is better served by setState(value => value.concat(someNewValue)); where the latter is using array semantics not iterator semantics.

Collapse
raphaelchaula profile image
Raphael Chaula Author • Edited

Good point.
But my point is when you want to update array state, don't act on the array state directly, you need to create another array from the state array and there are multiple solutions for that, I chose destructuring. that's all.