Interviewer: Tell me about your understanding of the react life cycle
Hello, this is Xiaochen. Today, let’s take a look at react
life cycle is executed at various stages. Did you encounter this problem during the interview? You can also study the react
source code system article. The article list is at the end of the article.
In the previous react
source code, we can divide the rendering process of the application into the mount
stage (application first rendering) and the update
stage (application status update). Whether in the mount
stage or the update
stage, there will be two sub-stages, one is render
One of the stages is commit
.
mount
:- In
render
based stagejsx
build new objectsworkInProgressFiber
tree, do not knowFiber
double buffering can view Past articles Fiber architecture , then the correspondingfiber
node labeledPlacement
, indicates that thisfiber
node needs to be inserted intodom
treefiber
nodes with side effects are added to a linked listEffect List
- In
commit
stage traversesrender
stages of formationEffect List
, performed on the corresponding listfiber
side node, suchPlacement
inserted, or performPassive
(useEffect
side effects). Apply these side effects to real nodes
- In
update
:- In the
render
stage, we will comparecurrent Fiber
according to the latest state ofjsx
objects, and then construct a newworkInProgressFiber
tree. The comparison process is thediff
algorithm. Thediff
algorithm is divided into single-node comparison and multi-node comparison. The article diff algorithm also undergoes the process of collecting side effects during the comparison process, which is to mark the difference and add it toEffect List
. The side effects of these comparisons are for example:Placement
(insert),Update
(update),Deletion
(Deleted) etc. - In the
commit
phase, it will also traverseEffect List
, and apply the side effects on thefiber
- In the
Why should we first talk about render
in the mount
and update
phases? This is because the react
life cycle is executed in these sub-phases. Let’s look at a picture.
render
stage:mount
: The component will first experienceconstructor
,getDerivedStateFromProps
,componnetWillMount
,render
update
: The component will first experiencecomponentWillReceiveProps
,getDerivedStateFromProps
,shouldComponentUpdate
,render
error
getDerivedStateFromError
will be called
commit
stagemount
: The component will experiencecomponnetDidMount
update
: The component will callgetSnapshotBeforeUpdate
,componnetDidUpdate
unMount
: CallcomponnetWillUnmount
error
: callcomponnetDidCatch
The red part is not recommended to be used. It should be noted that the execution order of each sub-phase of the commit
phase life cycle in mutation
Next, based on an example, explain the specific sequence of updates mount
and update
mount
wip Fiber
node in a depth-first manner, and then switch tocurrent Fiber
. In therender
constructor
,getDerivedStateFromProps
/componnetWillMount
andrender
each node will be executed incommit
. Time to execute the node'scomponnetDidMount
update
wip Fiber
tree will also be constructed in depth first. During the construction process, thediff
child node will be constructed. In therender
phase, if the existing node changes, such as c2 in the figure above, mark this nodeUpdate Flag
and executegetDerivedStateFromProps
render
, incommit
stage will be followed by the implementation of nodesgetSnapshotBeforeUpdate
,componnetDidUpdate
Video explanation (efficient learning): Click to learn
Previous React source code analysis articles:
3.react source code architecture
4. Source directory structure and debugging
Legacy and concurrent mode entry function
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。