DEV Community

Discussion on: Cheat Sheet for Updating Objects and Arrays in React State

Collapse
 
chamabreu profile image
Jan Manuel Brenner • Edited

Edit:
Thanks for this cheatsheet, nice to have it open when fighting with array states.

Mh, i learned to always get the old state like (dont focus on typos, just sketching):
const handleAdd = (todo) => {
setTodos((oldTodos) => {
return [...oldTodos, todo]
})
}

Why you dont use this?
Or is this approach only needed, if the new value depends on the old one?

Collapse
 
hkiepe profile image
Henrik Kiepe

would also be curious about the answer - but i think your approach is not a bad approach - just different. Why the author didnt took this into account would be inetersting ...