Answer the question
In order to leave comments, you need to log in
React - why use spread operator [...item] when adding data to state?
I'm learning react.
When adding data to a State, the following construction often comes across:
items = [...items, newItem]
Why is this used instead of arr.push(newItem)?
Answer the question
In order to leave comments, you need to log in
Because the data must be immutable (immutable). The spread operator returns a copy of the object without changing the original data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question