If we want to use arrays or objects in our React state, we have to create a copy of the value before modifying it. This is a cheat sheet on how to ...
For further actions, you may consider blocking this person and/or reporting abuse
Can you also add how to update array of objects
I agree
Just wrote How to update an array of objects in React state
This is a brilliant little recap for objects and arrays. Nicely done
For deleting key and value from object I prefer rest operator:
Signed up just to comment. This is so well set up and explained.
Most people would just put in the one-liners without explaining what they are doing at all.
🤙
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?
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 ...
Great article and recap : P
Magnifico hermano <3
In this snippet, the 'id' property is in brackets. Is this React syntax or some type of destructuring?
const handleAdd = (todo) => {
setTodos({...todos, [todo.id]: todo});
}
isn't this mutation?
const newTodos = [...todos];
newTodos[index] = todo;
The spread operator [...] creates a shallow copy and thus doesn't mutate the original state directly.