React Fiber and Virtual DOM Update Process
What is React Fiber?
React Fiber is a new reconciliation mechanism in React 16 that rethinks the rendering and update process. Its main goal is to make virtual DOM rendering incremental: break it into small "tasks", manage priorities and increase interface responsiveness even with large volumes of work.
Main Fiber Advantages
- Update priorities
- Higher priority tasks (animations, user actions) aren't blocked and can execute earlier.
- Render splitting
- Interrupt and resume render to avoid interface "freezing".
- Concurrent Mode
- Fiber serves as foundation for Concurrent Mode, allowing React to work asynchronously and adapt to current load.
Virtual DOM Update Process
React uses Virtual DOM to efficiently make changes to real DOM. This is implemented using Fiber, which describes component structure and tracks their changes.
Render Phase (Reconciliation)
-
Update initiation
- Every state change (via
setState,useState) enters update queue (Update Queue). - React can batch multiple
setStatecalls in one event.
- Every state change (via
-
Work In Progress tree creation
- React traverses Fiber tree, creating "draft" version of Virtual DOM (Work In Progress tree).
- At this stage render functions are called (or
rendermethods for class components), resulting in React getting new UI representation.
-
Diffing
- React compares (diff) new Virtual DOM version (Work In Progress) with old one.
- If element (component) types match, only changed attributes and child nodes are updated. If type changes, React rebuilds branch from scratch.
-
Working with keys (
key)- For lists, reordering and dynamic elements, keys (
key) help match original and new nodes, avoiding complete rebuild.
- For lists, reordering and dynamic elements, keys (
-
Side effects collection
- At this phase React determines which DOM nodes need to be added, removed or changed.
- "Effect list" is formed — what exactly should happen at next step.
-
Priority consideration (Fiber)
- Each update in Fiber has priority. If new task with higher priority appears (button click, scroll), React can interrupt current render and return to it later.
Important: In Render Phase real DOM changes aren't yet applied.
Commit Phase (Applying reconciliation)
- Applying changes to real DOM
- React takes "effect list" and precisely updates real DOM: changes attributes, adds or removes nodes.
- Lifecycle and hooks calls
- In class components this is
componentDidMount,componentDidUpdate,componentWillUnmount. - In functional components —
useEffect,useLayoutEffect.
- In class components this is
- Batch updates
- All changes within one event cycle are combined into one batch to avoid multiple small redraws.
Concurrent Mode and Fiber
Fiber is the foundation of Concurrent Mode, which provides more flexible render control mechanisms. In this mode React can partially show updated interface without blocking rest of application.
Concurrent Mode Advantages
- Smooth animations and transitions.
- Asynchronous processing of complex computations without interface "freezes".
- Priority rendering of important tasks.
Key Ideas
- Virtual DOM
- Stores and updates "draft" UI structure in memory, without heavy real DOM operations.
- Diffing
- Finds differences between old and new Virtual DOM versions.
- Reconciliation
- Applying changes to real DOM.
- Fiber
- Manages priorities, interrupts and resumes render, breaking it into small pieces.
Consider perf features:
If parent component renders, by default its child components also re-render. Use React.memo (or PureComponent in class components) if you want to optimize this behavior.