In React, is a component's lifecycle function called only once during a load or update?

in the component loading of React, the existing life cycle function will be called, similar to a life course, in which the life cycle function will only be called once?

Jun.06,2022

recommends this tutorial
this tutorial is a bit out of date, and some api will gradually become obsolete in the future. But in fact, the concept of life cycle has not changed. the focus is on the last figure .
A component is obviously divided into three phases from creation to destruction: instantiation-existence-destruction . Lifecycle functions during instantiation in
react are executed only once during instantiation. Lifecycle functions for the lifetime are repeatedly refreshed with each refresh of the component (say, setState). Similarly, the life cycle function of the destruction period is executed


only once when the last component is destroyed. After the fiber architecture, things like componentWillMount (), componentWillUpdate (), and componentWillReceiveProps () may all be called multiple times.


depends on whether your components have been destroyed, and some cases will continue to call


.

before the React15.x version, the React component has three processes: create, exist, and destroy. Except for the update life cycle in existence, which will be called multiple times (receiving the props from the parent component), the life cycle hooks of the other two phases will only be executed once.

in the React16.x version, fiber is introduced to optimize the process of the underlying rendering component. Once the original React15.x version is updated, it spares no effort to render the page to finish the task. That is, during the whole process of the rendered component, it will be divided into multiple time slice tasks. After the execution of each time slice, the control of the current main thread will be taken out and handed over to React to determine whether there are more urgent tasks at present, if other tasks are executed first. Tasks that have not been executed in the previous time will be discarded and restarted, which creates situations in which lifecycle functions will be executed multiple times.

fiber divides the lifecycle hook into two parts

reconciliation phase and commit phase

reconciliation phase :

componentWillMount
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate

commit phase :

componentDidMount
componentDidUpdate
componentWillUnmount

the life hook of the reconciliation phase may be called

multiple times.

refer to what is React Fiber


refer to the figure made by Dan Abramov

React Lifecycle

Menu