1. Two setState, how many times to call? As shown in the following code, there is a When a button is clicked, how many times will Answer: state = { count: 0 }; handleClick = () => { this.setState({ count: this.state.count + 1 }); this.setState({ count: this.state.count + 1 }); }; render() { console.log(`render`); return ( <> <div>Current count: {this.state.count}</div> <button onClick={this.handleClick}>add</button> </> ); } According to common sense, when the button is clicked for the first time, since Just run the above code in your browser: At the beginning, the page shows that the value of The reason is that React internally merges multiple This also explains why the final value of 2. Two setState, which one is called? But the above code does not verify which state = { count: 0 }; handleClick = () => { this.setState({ count: this.state.count + 1 }); this.setState({ count: this.state.count + 2 }); //Change to +2 }; render() { console.log(`render`); return ( <> <div>Current count: {this.state.count}</div> <button onClick={this.handleClick}>add</button> </> ); } Run it in the browser again: The results show that after clicking the button, the value of 3. Two setStates are placed in setTimeout? If we add a timer state = { count: 0 }; handleClick = () => { setTimeout(() => { this.setState({ count: this.state.count + 1 }); this.setState({ count: this.state.count + 2 }); }, 0); }; render() { console.log(`render`); return ( <> <div>Current count: {this.state.count}</div> <button onClick={this.handleClick}>add</button> </> ); } Running results: The results show that after clicking the button, the value of This is because calling 4. Conclusion Events used directly in React, such as React has a performance optimization mechanism for synthetic events and lifecycle functions. It will merge multiple If you bypass React's performance optimization mechanism and use This is the end of this article about how many times multiple setStates in React will be called. For more information about how many times multiple setStates in React will be called, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: select the best presets to create full compatibility with all browsersselect
>>: Theory: The two years of user experience
When mysql is running normally, it is not difficu...
Table of contents 1. Introduction 2. Deployment E...
When using Zabbix custom scripts to collect monit...
Remove the dotted box on the link Copy code The co...
How to save and exit after editing a file in Linu...
Table of contents Preface 1. Introduction to Axio...
Table of contents Linux netstat command 1. Detail...
Table of contents Preface 1. Binary Tree 1.1. Tra...
Table of contents 1. Use help information 2. Crea...
Copy code The code is as follows: <meta name=&...
This article shares the specific code of vue+echa...
This article mainly introduces the detailed expla...
Let our users choose whether to move forward or ba...
About password strength verification: [root@mysql...
Recently, many students have asked me about web p...