B
B
bormor2018-03-19 09:36:16
JavaScript
bormor, 2018-03-19 09:36:16

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

1 answer(s)
B
BugsCreator, 2018-03-19
@BugsCreator

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 question

Ask a Question

731 491 924 answers to any question