With regard to the problem of setState in react, I accidentally found it today. I would like to ask my friends to take a look at the small one. I really don't understand it.

first of all, here is my first piece of code:

let testValue = this.state.testValue;
    this.setState({testValue: testValue + 1});
    console.log(this.state.testValue);
    this.setState({testValue: testValue + 1});
    console.log(this.state.testValue);
    setTimeout(() => {
      // testValue
      this.setState({testValue:testValue + 1});
      console.log(this.state.testValue);
      this.setState({testValue:testValue + 1});
      console.log(this.state.testValue);
    }, 0);
The output in console on the

page is:

clipboard.png

:
let testValue = this.state.testValue;


clipboard.png

Why? at the beginning of ps:, the default value of testValue is 1. Please do not hesitate to comment.

Apr.03,2021

figured it out, because testValue! = = this.state.testValue in setTimeout makes sense


let testValue = this.state.testValue; / / testValue is 1, not responsive ha


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 (16), batch execution of setState in different places will behave differently.

the following is a STO link given in 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 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: How to realize a search engine of Baidu network disk

Next: Mongodb added fragment Times error.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e9fbb0-467c1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e9fbb0-467c1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?