In React
, setState
it can be either synchronous or asynchronous, depending on the execution timing and execution context
[1]. setState
The asynchrony does not mean that it is implemented by asynchronous code. In fact, the execution process and code are both synchronous, but the sequence of synthetic events and hook function calls is before the update, which results in incompatibility in synthetic events and hook functions. Changes are seen immediately , while in state
native events and are synchronoussetTimeout
setState
[2]. In React17
, setState
it is executed in batches, because it will be set before execution executionContext
, but if it is setTimeout
in functions such as event listeners, it will not be set executionContext
. At this time, setState
it will be executed synchronously. You can wrap a layer of batchUpdates
functions outside and set it manually excutionContext
Switch to asynchronous batch execution
[3]. In React
the rendering process, an object setState
will be created update
and hung on fiber
the object, and then performSyncWorkOnRoot
re-rendering will be scheduled. The order of a main task is: render
stage render
function execution --> commit
stage real DOM
replacement --> setState
callback function execution callback
, so , it can be seen setState
that the order of execution is render
after and commit
before
[4]. If ExecutionContext
it is 0, it means that there are no other tasks currently in progress, and it setState
is synchronous. In React
the source code, when ExecutionContext
it is 0, setState
it is synchronous
[5]. Batch update: Multiple sequences setState
are not executed synchronously one by one, but will be added to the queue one by one, and then executed together at the end. In synthetic events and life cycle hooks, setState
when the queue is updated, the merged state is stored , therefore, the value set before key
will be overwritten by the latter, and finally only sequential updates will be performed
In summary, setState
it can be either synchronous or asynchronous, depending on the timing of execution and the context of execution. In React
, if manual control setState
of asynchronous execution is required
That is, it is asynchronous in synthetic events and life cycle functions, and synchronous in native events and timers. It does not distinguish between setState
synchronous or asynchronous, but depends on whether it is batch update
in the middle
batchUpdates
Functions can be manually set excutionContext
to switch to asynchronous batch execution. At the same time, in synthetic events and life cycle hooks, setState
when the queue is updated, the merged state will be stored, so attention should be paid to key
the overwriting of values
(Add a community, expand the circle of contacts)
JS Interview Question 17 - Compare server-side rendering with client-side rendering
In what scenario is the event extension in JS interview question 16-Es6 used?
Framework - interview question 6 - talk about the hook function of Vue2 and Vue3
One article to understand the operation in the Internet