Interviewers often ask questions about React's life cycle

Interviewers often ask questions about React's life cycle

React Lifecycle

Two pictures to help you understand the life cycle of React

React Lifecycle (Old)

insert image description here

class Life extends React.Component{
      // Constructor constructor(props){
        console.log('Life constructor ---constructor');
        super(props)
        this.state={num:0}
      }
      // Calculate +1 function add=()=>{
        const {num} = this.state
        this.setState({num:num+1})
      }
      // Delete component death=()=>{
        ReactDOM.unmountComponentAtNode(document.getElementById('text'))
      }
      force=()=>{
        this.forceUpdate()
      }
      // Will mount componentWillMount(){
        console.log('Life will be mounted --- componentWillMount');
      }
      // Already mounted componentDidMount(){
        console.log('Life has been mounted --- componentDidMount');
      }
      // Delete trigger componentWillUnmount(){
        console.log('Life deletion trigger --- componentWillUnmount');
      }
      // Should the data be changed? shouldComponentUpdate(){
        console.log('Does Life change data --- shouldComponentUpdate');
        return true
      }
      // Data will be changed componentWillUpdate(){
        console.log('Life is about to change data---componentWillUpdate');
      }
      //Change datacomponentDidUpdate(){
        console.log('Life changes data---componentDidUpdate');
      }
      render(){
        console.log('Life---render');
        const {num} = this.state
        return(
          <div>
          <h1>Counter: {num}</h1> 
          <button onClick={this.add}>Click me +1</button> 
          <button onClick={this.death}>Delete</button> 
          <button onClick={this.force}>Do not change any state of data, force update</button> 
          </div>
        )
      }
    }
    // Render the page ReactDOM.render(<Life />, document.getElementById('text'))

Mounting steps

insert image description here

Update steps

insert image description here

delete

insert image description here

Summary: Initialization phase: triggered by ReactDOM.render() — initial rendering
1. constructor() ---構造器
2. componentWillMount() ---將要掛載
3. render() ---render
4. componentDidMount() ---掛載時: triggered by this.setSate() inside the component or re-rendering of the parent component
1. shouldComponentUpdate() ---是否要進行更改數據
2. componentWillUpdate() ---將要更新數據
3. render()
4. componentDidUpdate() ---更新數據and uninstall components: triggered by ReactDOM.unmountComponentAtNode()
componentWillUnmount() ---卸載

React Lifecycle (New)

Please add a description of the image

Three Phases of the Life Cycle (New)

Initialization phase: triggered by ReactDOM.render() — initial rendering
1. constructor()
2. getDerivedStateFromProps
3. render()
4. componentDidMount() update phase: triggered by this.setSate() inside the component or re-rendering of the parent component
1. getDerivedStateFromProps
2. shouldComponentUpdate()
3. render()
4. getSnapshotBeforeUpdate
5. componentDidUpdate() uninstalls the component: triggered by ReactDOM.unmountComponentAtNode()
1. componentWillUnmount()

Important hook

1.render: Initialize rendering or update rendering call
2.componentDidMount: Enable monitoring and send ajax request
3.componentWillUnmount: Do some finishing work, such as: clean up the timer

The hook that will be abandoned

1.componentWillMount
2.componentWillReceiveProps
3.componentWillUpdate

現在使用會出現警告,下一個大版本需要加上UNSAFE_前綴才能使用,以后可能會被徹底廢棄,不建議使用。

This concludes this article about the React lifecycle questions that interviewers often ask. For more content related to the React lifecycle, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief discussion on React Component life cycle functions
  • React new version life cycle hook function and usage detailed explanation
  • React State state and life cycle implementation method
  • React component life cycle example
  • React life cycle example analysis
  • React life cycle principle and usage notes
  • Comparison between Vue life cycle and react life cycle [recommended]
  • A brief discussion on the life cycle of components in React Native
  • React component life cycle detailed explanation
  • Commonplace js-react component life cycle

<<:  Summary of Linux system user management commands

>>:  Detailed explanation of the underlying encapsulation of Java connection to MySQL

Recommend

Does the website's text still need to be designed?

Many people may ask, does the text on the website...

How to build nfs service in ubuntu16.04

Introduction to NFS NFS (Network File System) is ...

Some Linux file permission management methods you may not know

Why do we need permission management? 1. Computer...

How to create, save, and load Docker images

There are three ways to create an image: creating...

Solution to the error problem of Vscode remotely connecting to Ubuntu

1. Background of the incident: Because of work ne...

Detailed explanation of MySQL redo log (redo log) and rollback log (undo logo)

Preface: The previous article described several c...

CSS3 transition to implement notification message carousel

Vue version, copy it to the file and use it <t...

Learn Vue middleware pipeline in one article

Often when building a SPA, you will need to prote...

Key issues and solutions for web page access speed

<br /> The website access speed can directly...

Vue imitates ElementUI's form example code

Implementation requirements The form imitating El...

JavaScript+html to implement front-end page sliding verification (2)

This article example shares the specific code of ...

Best Practices for Developing Amap Applications with Vue

Table of contents Preface Asynchronous loading Pa...

mysql5.7.21 utf8 encoding problem and solution in Mac environment

1. Goal: Change the value of character_set_server...

How to implement responsive layout with CSS

Implementing responsive layout with CSS Responsiv...

Detailed steps for building Portainer visual interface with Docker

In order to solve the problem mentioned last time...