React LifecycleTwo pictures to help you understand the life cycle of React
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 Update steps delete Summary: Initialization phase: triggered by ReactDOM.render() — initial rendering React Lifecycle (New) Three Phases of the Life Cycle (New) Initialization phase: triggered by ReactDOM.render() — initial rendering
1.render: Initialize rendering or update rendering call
1.componentWillMount
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:
|
<<: Summary of Linux system user management commands
>>: Detailed explanation of the underlying encapsulation of Java connection to MySQL
Many people may ask, does the text on the website...
Introduction to NFS NFS (Network File System) is ...
Why do we need permission management? 1. Computer...
There are three ways to create an image: creating...
1. Background of the incident: Because of work ne...
Preface: The previous article described several c...
Vue version, copy it to the file and use it <t...
Often when building a SPA, you will need to prote...
<br /> The website access speed can directly...
Implementation requirements The form imitating El...
This article example shares the specific code of ...
Table of contents Preface Asynchronous loading Pa...
1. Goal: Change the value of character_set_server...
Implementing responsive layout with CSS Responsiv...
In order to solve the problem mentioned last time...