Web Development

Mastering React's Execution Order: A Step-by-Step Guide to Lifecycle Phases

2026-05-02 13:12:08

Introduction

Understanding when different parts of your React component execute is crucial for writing predictable, bug-free code. Many developers are surprised that useLayoutEffect runs before useEffect, or that useEffect fires after the browser paints. This guide breaks down React's internal lifecycle into clear, actionable steps. By the end, you'll be able to trace exactly when your component's rendering, effects, and side-effect scheduling happen — and why.

Mastering React's Execution Order: A Step-by-Step Guide to Lifecycle Phases
Source: dev.to

What You Need

If you haven't already, consider reading the earlier parts of this series (Parts 1–4) to grasp the underlying Fiber architecture and scheduler — but this guide is designed to be self-contained.

Step 1: Understand the Three Phases of Every React Update

Every time React updates the UI, it goes through three distinct phases. Memorizing these is the foundation:

  1. Render Phase — React runs your component functions, compares the virtual DOM with the previous one, and figures out what changed. No DOM mutations happen here. This is where console.log('render') inside your function fires.
  2. Commit Phase — React takes the changes computed during the Render phase and applies them to the real DOM. This phase is synchronous and cannot be interrupted.
  3. Effects Phase — After the DOM is updated, React runs your side-effect functions (like useEffect and useLayoutEffect).

Key Insight: Effects are not part of rendering. They are separate tasks that happen after the DOM is already in place. This is why you can safely read DOM element properties inside an effect — the DOM reflects the current state by then.

Step 2: Recognize Where Your Code Runs in the Pipeline

Let's place common code patterns into the correct phase:

This step is about classification. Once you know which phase a piece of code belongs to, you can predict its execution order relative to other code.

Step 3: Trace the Exact Order for useEffect vs useLayoutEffect

Many developers get the order wrong. Here's the actual sequence for a component that uses both hooks:

  1. Render phase: The component function runs (including console.log('render')).
  2. Commit phase: React applies DOM changes synchronously.
  3. Effects phase: useLayoutEffect callbacks run immediately, still in the same synchronous block (before the browser can paint).
  4. Browser paint: The browser renders the new DOM to the screen.
  5. Effects phase (continued): useEffect callbacks are scheduled as a separate macro task and run after the paint.

So the console output for the example in the introduction will be:
renderlayout effecteffect.

Why this matters: If you need to read layout information (like element size) and update the DOM before the user sees it, use useLayoutEffect. For data fetching or network calls, useEffect is usually appropriate (and avoids blocking the paint).

Step 4: Understand How React Schedules useEffect

The useEffect callback does not run immediately after the commit. Instead, React schedules it using its internal Scheduler (the same one used for concurrent features like React.lazy and Suspense).

In practice, this means that between the commit and the useEffect execution, you can observe an intermediate state in the DOM where the updates are visible but the side effects haven't run yet. This is intentional to avoid blocking the paint for non-urgent work.

Mastering React's Execution Order: A Step-by-Step Guide to Lifecycle Phases
Source: dev.to

Step 5: Predict the Execution Timeline in Complex Scenarios

Now combine multiple components, hooks, and state updates. The same phases apply recursively for each update cycle. Here's how to trace any scenario:

  1. Identify the triggering update (state change, props change, mount).
  2. Observe the Render phase — all components in the tree run their render functions. Logs here appear first.
  3. Observe the Commit phase — the DOM is updated. You won't see logs here unless you've written a custom commit observer.
  4. Observe Effects phase (layout effects first, then scheduled effects)useLayoutEffect logs appear; then, after paint, useEffect logs appear.
  5. Note cleanup functions: If a component re-renders, any old effect's cleanup runs before the new effect's callback (in the same phase order: layout cleanups run before layout effects, effect cleanups run before effects).

For example, consider a parent with a child, each with both hooks. The order will be:

  1. Parent render
  2. Child render
  3. Parent useLayoutEffect cleanup (if any) → Child useLayoutEffect cleanup → Parent useLayoutEffect → Child useLayoutEffect
  4. Browser paint
  5. Parent useEffect cleanup → Child useEffect cleanup → Parent useEffect → Child useEffect

This nested order ensures that child effects always run before parent effects (because children commit first).

Tips

By mastering these steps, you can confidently predict when any piece of code executes in a React application. Understanding the lifecycle is not just academic — it prevents subtle bugs related to DOM access, state updates, and performance.

Explore

Top Tech Deals: Massive Savings on Samsung Tablets, Phones, Gaming Gear, and More What You Need to Know About Elon Musk confirms xAI used OpenAI’s models to ... Mozilla Upgrades Firefox's Free VPN with User-Selectable Server Locations How to Nominate a Fedora Community Champion: Mentor and Contributor Recognition 2026 Guide How to Scale Your Sovereign Private Cloud to Thousands of Nodes Using Azure Local