Sub topic 12
Fiber: the data structure behind the reconciler
This chapter assumes you know how the render and commit phases work, and that reconciliation compares the previous and next element trees. Here you will see what React uses internally to hold that work.
Why React needed Fiber
Before React 16, the reconciler walked the component tree using ordinary recursive function calls. Once a render started, it ran to the end without stopping. A large update could block the main thread for tens of milliseconds, so clicks and animations stalled.
Fiber is a rewrite of the reconciler. It replaces the call stack with a data structure React manages itself. Because React controls the traversal, it can pause, resume, or throw away work.
What a fiber is
A fiber is a plain JavaScript object. React creates one fiber for each element in the tree: each component, each host element such as div, and the root. A fiber is not the same as an element. An element is a short-lived description returned by your component. A fiber is a longer-lived record that React updates between renders.
A fiber holds roughly this information:
typeandkey, which reconciliation uses to decide whether to reuse the fiber.child,sibling, andreturn(the parent), which link fibers into a tree.pendingPropsandmemoizedProps, the new and last-used props.memoizedState, where hooks such asuseStatekeep their values.flags, which mark the work to do at commit time, such as a DOM insertion or an effect.alternate, a link to the other copy of this fiber (see below).
This explains a behavior from earlier chapters. State is stored on the fiber, not in your function. When type and position match, React reuses the fiber, so its state survives. When they don't, the fiber is discarded and the state goes with it.
Traversal without recursion
The fibers form a linked list-like tree. Each fiber points to its first child, its next sibling, and its parent. React processes one fiber at a time: it does the work for a fiber, moves to its child, and when there are no more children it moves to the sibling or back up to the parent.
Each unit of work is small. After each one, React can check whether it should yield, for example because a more urgent update has arrived.
Two trees: current and work-in-progress
React keeps two fiber trees.
- The current tree matches what is on screen.
- The work-in-progress tree is built during the render phase.
Each fiber's alternate field points to its counterpart in the other tree. React reuses these objects instead of allocating new ones on every render. When rendering finishes and the commit completes, React swaps the pointers, and the work-in-progress tree becomes the current tree.
This is why an interrupted render is safe. Nothing in the current tree has changed, and the DOM has not been touched. React can discard the work-in-progress tree and start over.
What Fiber changes for you
Fiber is an internal detail, and you never create or read fibers in application code. It matters because of what it makes possible:
| Capability | How Fiber supports it |
|---|---|
| Interruptible rendering | Work is split into small units, and React can yield between them. |
| Priorities | Urgent updates, such as typing, can start before a lower-priority render finishes. |
| Safe abandonment | Render work touches only the work-in-progress tree, not the DOM. |
The next chapter builds on this to explain transitions.
Exercise
A component's state resets when you change its key. Using what you know about fibers, explain why.
Show answer
Reconciliation compares type and key to decide whether to reuse an existing fiber. State lives in the fiber's memoizedState. When the key changes, React sees a different element, discards the old fiber and creates a new one with fresh state.
Exercise
Why can React throw away an in-progress render without leaving the screen in a half-updated state?
Show answer
Render-phase work happens on the work-in-progress fiber tree, while the DOM reflects the current tree. The DOM changes only during the commit phase, so discarding the work-in-progress tree leaves the screen untouched.