Render phase
createRoot().render()assigns the WorkInProgress Root (wipRoot) and setsnextUnitOfWork = wipRootworkLoopprocesses fibers in idle time usingrequestIdleCallback(typo fix: wasrequestIdelCallback)- Each fiber is processed by
performUnitOfWork, which:- If it's a Function Component:
- Set
wipFiber = fiber, initializewipFiber.hooks = []andhookIndex = 0 - Execute the function component (
fiber.type(fiber.props)) to get children - All
useStatecalls inside the component are executed, storing hooks towipFiber.hooks
- Set
- If it's a DOM element:
- Create the DOM node if not exists
- Call
reconcileChildren(wipFiber, elements)to:- Get the original fiber from
wipFiber.alternate.child - Compare new elements with old fibers
- Mark fibers with
effectTag:UPDATE,PLACEMENT, orDELETIONUPDATE: same type, reuse old fiber's DOM, update propsPLACEMENT: new element, create new DOMDELETION: old fiber no longer needed, push todeletionsqueue
- Link parent-child and sibling relationships (parent → first child, children as siblings)
- Get the original fiber from
- If it's a Function Component:
Commit phase
- When all unit works are done (
nextUnitOfWork === null),commitRoot()is called:- First,
commitWork()for all deletions (removes old DOMs) - Then,
commitWork(wipRoot.child)recursively processes all fibers:DELETION: remove the fiber's DOM from parentPLACEMENT: append the fiber's DOM to parentUPDATE: use the original fiber's DOM, update props and event listeners viaupdateDom()
- Set
currentRoot = wipRoot(save the committed root) - Reset
wipRoot = null
- First,
setStatere-assigns the current root as WorkInProgress Root (wipRoot) and setsnextUnitOfWork = wipRootagain
...following are the same as Initial render phase
- Render the entire component tree recursively at once
- Directly create and append DOM nodes
- No diff mechanism, no optimization
- Split each DOM's render process unit by unit (element by element):
- Reconcile phase: According to changes, mark each fiber's next action (
effectTag)- Compare new elements with old fibers
- Mark as
UPDATE,PLACEMENT, orDELETION
- Commit phase: Execute the action by each fiber (
commitWork)- Apply changes to the real DOM based on
effectTag
- Apply changes to the real DOM based on
- Reconcile phase: According to changes, mark each fiber's next action (
- Create the Work Loop (
workLoopfunction) and onlyperformUnitOfWorkin idle time usingrequestIdleCallback
- Add
useStatehook implementation- Store hooks in each Functional component's fiber (
wipFiber.hooks) - Execute all pending actions from the previous render (from
oldHook.queue) to update the state - Provide the
setStatefunction to queue updates and trigger the next render loop
- Store hooks in each Functional component's fiber (
