0
0
ReactHow-ToIntermediate · 4 min read

How React Fiber Works: Understanding React's Reconciliation Engine

React Fiber is a reimplementation of React's core algorithm that breaks rendering work into small units called fibers. It allows React to pause, prioritize, and resume work, making UI updates smoother and more responsive.
📐

Syntax

React Fiber is an internal engine, so you don't write Fiber code directly. Instead, you write React components as usual, and Fiber manages rendering behind the scenes.

Key parts of Fiber include:

  • Fiber Node: Represents a piece of the UI tree.
  • Reconciliation: The process Fiber uses to compare old and new UI trees.
  • Work Loop: Breaks rendering into small tasks to avoid blocking the browser.
jsx
function MyComponent() {
  return <div>Hello Fiber</div>;
}

// React Fiber handles rendering this component efficiently behind the scenes.
Output
<div>Hello Fiber</div>
💻

Example

This example shows a simple React component that updates state. React Fiber breaks the update into small units, allowing the UI to stay responsive even during complex updates.

jsx
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    // React Fiber schedules this update efficiently
    setCount(prevCount => prevCount + 1);
  }

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}

export default Counter;
Output
Count: 0 [Button labeled 'Increment'] (Clicking the button increases the count without freezing the UI)
⚠️

Common Pitfalls

Because React Fiber works behind the scenes, common mistakes include:

  • Expecting synchronous updates: Fiber may pause and resume work, so updates are often asynchronous.
  • Mutating state directly: This breaks Fiber's ability to track changes efficiently.
  • Blocking the main thread with heavy computations: Fiber can pause rendering but cannot fix blocking JavaScript.

Always use setState or hooks to update state and avoid heavy synchronous code in render.

jsx
/* Wrong: Mutating state directly */
const obj = { value: 1 };
obj.value = 2; // React Fiber won't detect this change

/* Right: Using setState or hooks */
const [value, setValue] = React.useState(1);
setValue(2); // Fiber schedules update properly
📊

Quick Reference

React Fiber Cheat Sheet:

  • Fiber Node: Unit of work representing UI element.
  • Reconciliation: Compares old and new UI trees to update efficiently.
  • Scheduling: Prioritizes updates to keep UI responsive.
  • Asynchronous Rendering: Allows React to pause and resume work.
  • State Updates: Use setState or hooks for Fiber to track changes.

Key Takeaways

React Fiber breaks rendering into small units to keep UI responsive.
Fiber allows React to pause, prioritize, and resume rendering work.
Always update state immutably using hooks or setState for Fiber to work correctly.
Heavy synchronous code can block Fiber's scheduling and freeze the UI.
Fiber is internal; write React components normally and let Fiber optimize rendering.