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

What are HTML inline elements and block-level elements and their differences

I remember a question the interviewer asked durin...

Analysis of the process of deploying pure HTML files in Tomcat and WebLogic

1. First, the pure HTML file must have an entry i...

CSS3 simple cutting carousel picture implementation code

Implementation ideas First, create a parent conta...

VMware15 installation of Deepin detailed tutorial (picture and text)

Preface When using the Deepin user interface, it ...

Briefly explain the use of group by in sql statements

1. Overview Group by means to group data accordin...

Analysis of Sysbench's benchmarking process for MySQL

Preface 1. Benchmarking is a type of performance ...

Mysql 5.6 adds a method to modify username and password

Log in to MySQL first shell> mysql --user=root...

MySQL full-text search usage examples

Table of contents 1. Environmental Preparation 2....

Example of using CSS3 to achieve shiny font effect when unlocking an Apple phone

0. Introduction August 18, 2016 Today, I noticed ...

Summary of mysqladmin daily management commands under MySQL (must read)

The usage format of the mysqladmin tool is: mysql...

js and jquery to achieve tab status bar switching effect

Today we will make a simple case, using js and jq...

Vue encapsulation component tool $attrs, $listeners usage

Table of contents Preface $attrs example: $listen...

Tutorial on using Multitail command on Linux

MultiTail is a software used to monitor multiple ...

Basic usage of exists, in and any in MySQL

【1】exists Use a loop to query the external table ...