In React, EffectList is traversed to perform node operations, lifecycle methods, and Effect methods. EffectList can be compared to the colorful lights hanging on a Christmas tree, and this Christmas tree is the Fiber tree. Why does EffectList exist? For example, there are some Fiber nodes in a Fiber tree that need to execute the EffectList solves this problem. During the construction of the Fiber tree, whenever the EffectList Collection EffectList is a one-way linked list. The construction of the Fiber tree is depth-first, that is, the child Fiber nodes are built downwards first, and after the child nodes are built, the parent Fiber nodes are built upwards, so the child Fiber nodes are always in front in the EffectList. The operation of completing the construction of the Fiber node is executed in the The simplified code is as follows. function completeUnitOfWork(unitOfWork: Fiber): void { let completedWork = unitOfWork; do { const current = completedWork.alternate; const returnFiber = completedWork.return; let next = completeWork(current, completedWork, subtreeRenderLanes); // effect list construction if ( returnFiber !== null && (returnFiber.flags & Incomplete) === NoFlags ) { // Copy layer by layer if (returnFiber.firstEffect === null) { returnFiber.firstEffect = completedWork.firstEffect; } if (completedWork.lastEffect !== null) { // This means that the current node is a sibling node, the child node has an effect, and returnFiber.lastEffect has been assigned a value if (returnFiber.lastEffect !== null) { // Effect of connecting sibling nodes returnFiber.lastEffect.nextEffect = completedWork.firstEffect; } returnFiber.lastEffect = completedWork.lastEffect; } const flags = completedWork.flags; // This fiber node has an effect if (flags > PerformedWork) { // The current node has an effect connected to the effect list if (returnFiber.lastEffect !== null) { returnFiber.lastEffect.nextEffect = completedWork; } else { // The case where returnFiber does not have firstEffect is the first time it encounters a node with effect returnFiber.firstEffect = completedWork; } returnFiber.lastEffect = completedWork; } } // Traverse the sibling elements and return to the parent const siblingFiber = completedWork.sibling; if (siblingFiber !== null) { workInProgress = siblingFiber; return; } completedWork = returnFiber; workInProgress = completedWork; } while (completedWork !== null); } EffectList actually works like a bubble, collecting data layer by layer to the upper layer, starting from the first node with As in the following structure, if each <div id="1"> <div id="4"/> <div id="2"> <div id="3"/> </div> </div> The final EffectList is firstEffect => div4 lastEffect => div1 Because the Fiber tree is constructed depth-first, all EffectList traversal starts from firstEffect => div4 div4.nextEffect => div3 div3.nextEffect => div2 div2.nextEffect => div1 EffectList at the first render In React, there is a performance optimization for the first Mount, in which the Fiber node's EffectList does not include let firstEffect; // Connect the root node finishedWork as well if (finishedWork.flags > PerformedWork) { if (finishedWork.lastEffect !== null) { finishedWork.lastEffect.nextEffect = finishedWork; firstEffect = finishedWork.firstEffect; } else { firstEffect = finishedWork; } } else { // The root node has no effect. firstEffect = finishedWork.firstEffect; } Traversal of EffectListEffectList is mainly used for the execution of Layout phase life cycle methods and DOM operations. // Process getSnapshotBeforeUpdate and schedule useEffect nextEffect = firstEffect; do { commitBeforeMutationEffects(); } while (nextEffect !== null); //DOM operation nextEffect = firstEffect; do { commitMutationEffects(root, renderPriorityLevel); } while (nextEffect !== null); // Execution of life cycle methods nextEffect = firstEffect; do { commitLayoutEffects(root, lanes); } while (nextEffect !== null); In these three methods in the Layout stage, Summarize EffectList is not a global variable. During the creation of the Fiber tree, Fiber nodes with Since the collection process is depth-first, the children will be collected first, so the children will be operated first during traversal. Therefore, if an interviewer asks which of the child and parent life cycles or The above is a brief analysis of the details of EffectList in React. For more information about EffectList in React, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to use multi-core CPU to speed up your Linux commands (GNU Parallel)
>>: Django+mysql configuration and simple operation database example code
This article uses a specific example to introduce...
Preface: In MySQL, the CONCAT() function is used ...
Today I looked at some things related to data bac...
Table of contents Use Cases Reactive API related ...
Installation environment: CentOS7 64-bit, MySQL5....
[Usage and function of mysql cursor] example: The...
Table of contents 1. Eclipse configures Tomcat 2....
Table of contents Preface Hello World image Set b...
Table of contents Preface ErrorBoundary Beyond Er...
Table of contents 1. Introduction 2. Several ways...
This article shares with you how to use Vue to dr...
The following code introduces MySQL to update som...
introduction It is okay to add or not add a semic...
Table of contents UNION Table initialization Exec...
Table of contents 1. What is grub encryption 2. g...