What's the problem with react's setState in the timer?

state= {

val:0

}
put on the timer to execute setState twice in a row
clipboard.png

clipboard.png
setState
clipboard.png

clipboard.png

what I see is that the setState twice put in the timer is Synchronize , and the twice setState is asynchronous without a timer. Can you help me explain this?

Mar.13,2021

gives you a reference


https://reactjs.org/docs/reac.
setState() enqueues changes to the component state and tells React that this component and its children need to be re-rendered with the updated state. This is the primary method you use to update the user interface in response to event handlers and server responses.
the above is the official document's explanation of batch setState, saying only that setState will be queued, but in fact, in the current version, batch execution of setState in different places will behave differently.

the following is a link from the official documentation indicating when setState will be processed in batches
In depth: When and why are setState () calls batched? (learn more about when and why setState () calls are merged)

Currently (React 16 and earlier), only updates inside React event handlers are batched by default. There is an unstable API to force batching outside of event handlers for rare cases when you need it.

In future versions (probably React 17 and later), React will batch all updates by default so you won't have to think about this. As always, we will announce any changes about this on the React blog and in the release notes.
now (React 16 and before), by default, only setState written directly in the < del > react lifecycle < / del > React event handlers will be merged for future versions of
(probably starting from React 17,), React will merge all setState by default

another link given in the official documentation below

Previous: The problem of setTimeout minimum interval 4ms

Next: How to use node.js to send data to the tcp server at intervals?

Menu