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
HTML5 is the next version of the HTML standard. M...
Front-end is a tough job, not only because techno...
Version 1.4.2 Official Documentation dockerhub st...
Table of contents Table/index.js Table/model/inde...
During the Olympic Games, IE 8 Beta 2 will be rele...
Table of contents 1. Installation 2. There is no ...
First, let's talk about why we use provide/in...
ps: The environment is as the title Install possi...
This article example shares the specific code of ...
When using MySQL to run certain statements, a dea...
This article shares the installation tutorial of ...
This article shares the specific code of JavaScri...
1. What is CSS Animations is a proposed module fo...
Let's look at the case first. Use vue+swiper ...
Use of stored procedure in parameters IN paramete...