⚛️ React Q55 / 113

What are render phases in React?

AI-Powered Answer ✓ Answered

In React, rendering is the process of updating the UI. This process is divided into distinct phases, primarily the Render Phase and the Commit Phase, which together form React's reconciliation algorithm. Understanding these phases is crucial for writing efficient and predictable React applications.

Overview of Render Phases

React's reconciliation process, which determines how to update the DOM to match your component tree, is broken down into two main phases:

  • Render Phase (or Reconciliation Phase): This is where React calls your components' render methods or function components to figure out what needs to be rendered. It's a pure and side-effect-free phase.
  • Commit Phase: This is where React applies the changes identified in the render phase to the actual DOM and runs lifecycle methods or effects.

1. Render Phase

The Render Phase is concerned with calculating what changes need to be made to the UI. During this phase, React performs the following actions:

  • Calling Components: React traverses your component tree, calling the render() method of class components or executing function components.
  • Diffing: It compares the newly returned elements (from the current render) with the previously rendered elements (from the previous render) to identify differences. This is known as 'diffing' the Virtual DOM.
  • Preparing Changes: It builds a list of all the necessary updates, insertions, deletions, and attribute changes that need to be applied to the actual DOM.

Crucially, the Render Phase must be pure: it should not cause any side effects (e.g., modifying the DOM directly, making network requests, or updating state). Because this phase can be paused, interrupted, or even restarted (especially with features like Concurrent Mode), any side effects performed here could lead to inconsistent or buggy behavior. Strict Mode helps identify impure renders by intentionally double-invoking components during development.

2. Commit Phase

The Commit Phase is where React takes the changes identified during the Render Phase and applies them to the browser's DOM. This phase is always synchronous and cannot be interrupted. During the Commit Phase, React performs these steps:

  • DOM Updates: React updates the actual DOM to reflect the new UI state.
  • Lifecycle Methods/Effects: After DOM updates, React runs lifecycle methods for class components (e.g., componentDidMount, componentDidUpdate, componentWillUnmount) and executes useLayoutEffect and useEffect hooks for functional components. This is the safe place to perform side effects.

Side effects, such as network requests, DOM manipulations, or subscriptions, should only be performed in the Commit Phase (e.g., within useEffect or componentDidMount/componentDidUpdate).

Summary of Render Phases Flow

  • Trigger a Render: A render is triggered by a state update (setState, useState), prop changes, or forced updates (forceUpdate).
  • Render Phase: React calls your component functions/methods to render (potentially multiple times in Concurrent Mode) and calculates the differences.
  • Commit Phase: React applies the calculated changes to the DOM and then runs useLayoutEffect hooks, followed by browser painting, and finally useEffect hooks.