Code analysis of synchronous and asynchronous setState issues in React

Code analysis of synchronous and asynchronous setState issues in React

React originated as an internal project at Facebook. The emergence of React is a revolutionary innovation. React is a subversive front-end framework. React officially introduces it as: a declarative, efficient, flexible JavaScript library for creating user interfaces. Even though React's main function is to build UI, the gradual growth of the project has made React a WebApp solution that covers both the front-end and back-end.

Angular uses the watcher object, Vue uses the observer mode, and react uses the state. They each have their own characteristics. There is no good or bad, only different choices due to different needs.

React's official website: https://reactjs.org/GitHub

The address is: https://github.com/facebook/react

1. In React, event handling functions controlled by React, such as onClick, onChange, etc., setState is asynchronous

import React, { Component } from 'react';

export default class Input extends Component {
    constructor(props) {
        super(props);    

        this.state={
            name: 'hello'
        }    
    }

    _onChange(e) {
        this.setState({
            name:' world'
        })

        console.log(this.state.name); //hello
    }

  render () {
    return (
      <div className='cp'>
        <input className='cp-input' value={this.state.name} onChange={this._onChange.bind(this)} type="text"/>                
      </div>
    );
  }
}

2. In native JS listening events, such as addEventListener, setState is synchronous

import React, { Component } from 'react';

export default class Input extends Component {
    constructor(props) {
        super(props);    

        this.state={
            name: 'hello'
        }    
    }

    _onChange(e) {
        // do something
    }


    componentDidMount() {
        let input = document.querySelector('.cp-input');
        input.addEventListener('click', ()=>{
            this.setState({
                name:' world'
            })

            console.log(this.state.name); //world
        })
    }

  render () {
    return (
      <div className='cp'>
        <input className='cp-input' value={this.state.name} onChange={this._onChange.bind(this)} type="text"/>                
      </div>
    );
  }
}

3. In setTimeout, setStatet is synchronous

import React, { Component } from 'react';

export default class Input extends Component {
    constructor(props) {
        super(props);    

        this.state={
            name: 'hello'
        }    
    }

    _onChange(e) {
        // do something
    }


    componentDidMount() {
        setTimeout(()=>{
            this.setState({
                name:' world'
            })
            console.log(this.state.name); //world
        }, 1000)
    }

  render () {
    return (
      <div className='cp'>
        <input className='cp-input' value={this.state.name} onChange={this._onChange.bind(this)} type="text"/>                
      </div>
    );
  }
}

The above is the detailed content of parsing the detailed analysis of the synchronous and asynchronous code of setState in React. For more information about React setState synchronous and asynchronous, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Understanding of the synchronous or asynchronous problem of setState in React
  • The use of setState in React and the use of synchronous and asynchronous
  • Use of setState synchronous and asynchronous scenarios in React

<<:  MySQL statement arrangement and summary introduction

>>:  How to install and use Cockpit on CentOS 8/RHEL 8

Recommend

The docker-maven-plugin plugin cannot pull the corresponding jar package

When using the docker-maven-plugin plug-in, Maven...

Docker realizes the connection with the same IP network segment

Recently, I solved the problem of Docker and the ...

Detailed explanation of the relationship between React and Redux

Table of contents 1. The relationship between red...

Detailed explanation of padding and abbreviations within the CSS box model

As shown above, padding values ​​are composite at...

Analysis of the principle of centering elements with CSS

It is a very common requirement to set the horizo...

Summary of several common logs in MySQL

Preface: In the MySQL system, there are many diff...

Example of using negative margin to achieve average layout in CSS

For evenly distributed layouts, we generally use ...

Analysis of Nginx Rewrite usage scenarios and configuration methods

Nginx Rewrite usage scenarios 1. URL address jump...

Web componentd component internal event callback and pain point analysis

Table of contents Written in front What exactly i...

Zabbix configures DingTalk's alarm function with pictures

Implementation ideas: First of all, the alarm inf...

Explanation of the precautions for Mysql master-slave replication

1. Error error connecting to master 'x@xxxx:x...

Analyze MySQL replication and tuning principles and methods

1. Introduction MySQL comes with a replication so...

Detailed explanation of the data responsiveness principle of Vue

This article is mainly for those who do not under...