1. setState() Description1.1 Update datasetState() is used to update data asynchronously You can call setState() multiple times, and it will only trigger a re-rendering once. import React from 'react' import ReactDOM from 'react-dom' class Opp extends React.Component { state = { count: 1, } handleClick = () => { // Asynchronously update data this.setState({ count: this.state.count + 1, }) this.setState({ count: this.state.count + 1, }) console.log(this.state.count) // 1 } render() { return ( <div> <h1>Counter: {this.state.count}</h1> <button onClick={this.handleClick}>+1</button> </div> ) } } ReactDOM.render(<Opp />, document.getElementById('root')) 1.2 Recommended syntaxUse the setState((state, props)=>{}) syntax
import React from 'react' import ReactDOM from 'react-dom' class Opp extends React.Component { state = { count: 1, } handleClick = () => { /* // Asynchronously update data this.setState({ count: this.state.count + 1, }) console.log(this.state.count) //1 this.setState({ count: this.state.count + 1, }) console.log(this.state.count) //1 */ // Recommended syntax this.setState((state, props) => { return { count: state.count + 1, } }) this.setState((state, props) => { console.log('Second call:', state) //2 return { count: state.count + 1, } }) console.log(this.state.count) // 3 } render() { return ( <div> <h1>Counter: {this.state.count}</h1> <button onClick={this.handleClick}>+1</button> </div> ) } } ReactDOM.render(<Opp />, document.getElementById('root')) 1.3 Second Parameter
import React from 'react' import ReactDOM from 'react-dom' class Opp extends React.Component { state = { count: 1, } handleClick = () => { this.setState( (state, props) => { return { count: state.count + 1, } }, // Execute immediately after the state is updated and re-rendered () => { console.log('Status update completed:', this.state.count) // 2 console.log(document.getElementById('title').innerText) // Counter: 2 document.title = 'Updated count is:' + this.state.count } ) console.log(this.state.count) //1 } render() { return ( <div> <h1 id='title'>Counter: {this.state.count}</h1> <button onClick={this.handleClick}>+1</button> </div> ) } } ReactDOM.render(<Opp />, document.getElementById('root')) 2. JSX syntax conversion process
import React from 'react' import ReactDOM from 'react-dom' //Conversion process of JSX syntax// const element = <h1 className='greeting'>Hello JSX</h1> const element = React.createElement( 'h1', { className: 'greeting', }, 'Hello JSX' ) console.log(element) ReactDOM.render(element, document.getElementById('root')) 3. Component update mechanism
4. Component performance optimization4.1 Reducing state
4.2 Avoid unnecessary re-rendering
import React from 'react' import ReactDOM from 'react-dom' class Opp extends React.Component { state = { count: 0, } handleClick = () => { this.setState((state) => { return { count: this.state.count + 1, } }) } //Hook function shouldComponentUpdate(nextProps, nextState) { // Return false to prevent the component from re-rendering // return false // The latest state console.log('latest state', nextState) // Status before update console.log(this.state) // Return true, the component re-renders return true } render() { console.log('Component updated') return ( <div> <h1>Counter: {this.state.count}</h1> <button onClick={this.handleClick}>+1</button> </div> ) } } ReactDOM.render(<Opp />, document.getElementById('root')) Example: Random Numbers Via nextState import React from 'react' import ReactDOM from 'react-dom' // Generate random numbers class Opp extends React.Component { state = { number: 0, } handleClick = () => { this.setState((state) => { return { number: Math.floor(Math.random() * 3), } }) } // The random numbers generated twice may be the same, so there is no need to re-render shouldComponentUpdate(nextState) { console.log('Latest state:', nextState, 'Current state:', this.state) return nextState.number !== this.state.number /* if ( nextState.number !== this.state.number) { return true } return false*/ } render() { console.log('render') return ( <div> <h1>Random number: {this.state.number}</h1> <button onClick={this.handleClick}>Regenerate</button> </div> ) } } ReactDOM.render(<Opp />, document.getElementById('root')) Via nextState import React from 'react' import ReactDOM from 'react-dom' // Generate random numbers class Opp extends React.Component { state = { number: 0, } handleClick = () => { this.setState((state) => { return { number: Math.floor(Math.random() * 3), } }) } render() { return ( <div> <NumberBox number={this.state.number} /> <button onClick={this.handleClick}>Regenerate</button> </div> ) } } class NumberBox extends React.Component { shouldComponentUpdate(nextProps) { console.log('latest props:', nextProps, 'current props:', this.props) return nextProps.number !== this.props.number } render() { console.log('subcomponent render') return <h1>Random number: {this.props.number}</h1> } } ReactDOM.render(<Opp />, document.getElementById('root')) SummarizeThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Docker container monitoring principle and cAdvisor installation and usage instructions
1. Image formats supported on the WEB: GIF: can s...
CSS3 syntax: (1rem = 100px for a 750px design) @m...
This is a website I imitated when I was self-stud...
1. Introduction After MySQL is started, BufferPoo...
Table of contents Environmental Description Insta...
We often need to control the hidden, transparent ...
XML/HTML CodeCopy content to clipboard < div c...
Table of contents 1. Download steps 2. Configure ...
In most cases, MySQL does not support Chinese whe...
【SQL】SQL paging query summary The need for paging...
We don’t often encounter 404 pages when we browse...
The effect is as follows: analyze 1. Here you can...
I updated MySQL 8.0 today. The first problem: Nav...
Install boost There are many ways to call C/C++ f...
This article example shares the specific code of ...