G
G
GreenMan18992019-04-25 01:50:06
React
GreenMan1899, 2019-04-25 01:50:06

How does setState work?

I can’t find such information on the work of setState: are the fields of the state object inside setState updated asynchronously with each other or not? And if you cause a change, for example, of the same field of the counter counter to +1 within one setState, will they be incremented asynchronously?
5cc0eb5b4e5ad765945571.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-04-25
@GreenMan1899

Duplicate properties in an object literal override each other. The value of the latter gets into the created object. Example.
So the object returned by the function call is:

prevState => ({
  counter: prevState.counter + 1,
  counter: prevState.counter + 1,
})

with , will be generated as:prevState.counter === 0
{
  counter: 0 + 1,
  counter: 0 + 1,  // результат этого выражения попадет в созданный объект
}

and the output will be an object: It will be mixed into the new state object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question