search
HomeWeb Front-endFront-end Q&AThe react life cycle is divided into several stages

The react life cycle is divided into 3 stages; they are: 1. The creation stage, also known as the initialization stage, which represents the process when the component is rendered in the DOM tree for the first time; 2. The update stage, also known as the initialization stage. It is called the existence stage, which represents the process in which the component is re-rendered; 3. The unloading stage, also called the destruction stage, represents the process in which the component is deleted from the DOM.

The react life cycle is divided into several stages

The operating environment of this tutorial: Windows 10 system, react17.0.1 version, Dell G3 computer.

The react life cycle is divided into several stages

The concept of life cycle is widely used, especially in many fields such as economy, environment, technology, society and so on. Its basic The meaning can be commonly understood as the entire process of "cradle-to-grave"

Like Vue, the entire component life cycle of React includes creation, initialization of data, compilation of templates, and mounting of Dom →Rendering, update→Rendering, uninstallation and a series of processes

Process

Here we mainly talk about the life cycle after react16.4, which can be divided into three stages:

  • Creation Phase

  • Update Phase

  • Uninstall Phase

Creation Phase

The creation phase is mainly divided into the following life cycle methods:

  • Constructor (don’t ask me why I use Chinese, because English will be swallowed up, it’s too difficult)

  • getDerivedStateFromProps

  • render

  • ##componentDidMount

Constructor

Method automatically called during the instance process. Inside the method, props from the parent component are obtained through the super keyword

In this method, the usual operations are Initialize the state or mount the method on this

getDerivedStateFromProps

This method is a new life cycle method and is a static method, so the component cannot be accessed Example

Execution timing: During the component creation and update phase, whether it is a props change or a state change, it will also be called

before each render method, and the first parameter is the one that is about to be updated. props, the second parameter is the state of the previous state. You can compare props and state to add some restrictions to prevent useless state updates

This method needs to return a new object as the new state or return null Indicates that the state does not need to be updated

render

A method that class components must implement for rendering the DOM structure and can access component state and prop attributes

Note: Do not setState in render, otherwise it will trigger an infinite loop and cause memory crash

componentDidMount

The component is executed after it is mounted to the real DOM node, which is executed in the render method Then execute

This method is mostly used to perform some data acquisition, event monitoring and other operations

Update stage

The functions in this stage are mainly the following methods:

  • getDerivedStateFromProps

  • shouldComponentUpdate

  • render

  • ##getSnapshotBeforeUpdate
  • componentDidUpdate
getDerivedStateFromProps

This method is introduced as above

shouldComponentUpdate

Used to tell the component itself whether it needs to re-render the component based on the current props and state. By default, true is returned.

Execution timing: It will be called when new props or state are reached, by returning true Or false to tell whether the component is updated or not

In general, it is not recommended to perform deep comparisons in this cycle method, which will affect efficiency

At the same time, setState cannot be called, otherwise it will cause an infinite loop to call update

render

The introduction is as above

getSnapshotBeforeUpdate

This periodic function is executed after render. The DOM element has not been updated yet

The Snapshot value returned by this method is passed in as the third parameter of componentDidUpdate

getSnapshotBeforeUpdate(prevProps, prevState) {
    console.log('#enter getSnapshotBeforeUpdate');
    return 'foo';
}
componentDidUpdate(prevProps, prevState, snapshot) {
    console.log('#enter componentDidUpdate snapshot = ', snapshot);
}

The purpose of this method is to obtain some information before the component is updated, such as the component The scroll position and the like, after the component is updated, some UI visual states can be restored based on this information

componentDidUpdate

Execution timing: Triggered after the component update is completed

In this method, you can perform corresponding operations based on the changes in props and state before and after, such as obtaining data, modifying DOM styles, etc.

Unloading phase

componentWillUnmount

This method is used to clean up some registered listening events, or cancel subscribed network requests before component uninstallation.

Once a component instance is uninstalled, it will not be mounted again. , and can only be recreated

Recommended learning: "

react video tutorial

"

The above is the detailed content of The react life cycle is divided into several stages. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
react中canvas的用法是什么react中canvas的用法是什么Apr 27, 2022 pm 03:12 PM

在react中,canvas用于绘制各种图表、动画等;可以利用“react-konva”插件使用canvas,该插件是一个canvas第三方库,用于使用React操作canvas绘制复杂的画布图形,并提供了元素的事件机制和拖放操作的支持。

react中antd和dva是什么意思react中antd和dva是什么意思Apr 21, 2022 pm 03:25 PM

在react中,antd是基于Ant Design的React UI组件库,主要用于研发企业级中后台产品;dva是一个基于redux和“redux-saga”的数据流方案,内置了“react-router”和fetch,可理解为应用框架。

React是双向数据流吗React是双向数据流吗Apr 21, 2022 am 11:18 AM

React不是双向数据流,而是单向数据流。单向数据流是指数据在某个节点被改动后,只会影响一个方向上的其他节点;React中的表现就是数据主要通过props从父节点传递到子节点,若父级的某个props改变了,React会重渲染所有子节点。

react中为什么使用nodereact中为什么使用nodeApr 21, 2022 am 10:34 AM

因为在react中需要利用到webpack,而webpack依赖nodejs;webpack是一个模块打包机,在执行打包压缩的时候是依赖nodejs的,没有nodejs就不能使用webpack,所以react需要使用nodejs。

react是组件化开发吗react是组件化开发吗Apr 22, 2022 am 10:44 AM

react是组件化开发;组件化是React的核心思想,可以开发出一个个独立可复用的小组件来构造应用,任何的应用都会被抽象成一颗组件树,组件化开发也就是将一个页面拆分成一个个小的功能模块,每个功能完成自己这部分独立功能。

react和reactdom有什么区别react和reactdom有什么区别Apr 27, 2022 am 10:26 AM

react和reactdom的区别是:ReactDom只做和浏览器或DOM相关的操作,例如“ReactDOM.findDOMNode()”操作;而react负责除浏览器和DOM以外的相关操作,ReactDom是React的一部分。

react中forceupdate的用法是什么react中forceupdate的用法是什么Apr 19, 2022 pm 12:03 PM

在react中,forceupdate()用于强制使组件跳过shouldComponentUpdate(),直接调用render(),可以触发组件的正常生命周期方法,语法为“component.forceUpdate(callback)”。

react与vue的虚拟dom有什么区别react与vue的虚拟dom有什么区别Apr 22, 2022 am 11:11 AM

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.