Course contentsShow
← React
Lesson 438 of 2,39612. Rendering and BatchingFree lesson

The Render Cycle Timeline

Understanding the sequence: event handler runs → state updates queued → render → commit → screen updates.

The Render Cycle Timeline

What you'll learn: The exact sequence of events from user interaction to screen update in React.

The Complete Timeline

When a user clicks a button or triggers an event in your React app, a very specific sequence unfolds:

  1. Event handler runs — Your onClick or other handler executes immediately
  2. State updates queued — Calls to setState schedule updates, they don't apply yet
  3. Render phase — React calculates what changed by calling your component functions
  4. Commit phase — React applies those changes to the actual DOM
  5. Screen updates — The browser paints the new DOM to the screen

Why This Order Matters

Think of it like ordering at a restaurant: you tell the waiter what you want (event handler), the kitchen prepares multiple orders together (batching), they cook the meal (render), plate it (commit), and finally serve it (screen update).

The key insight: your event handler finishes completely before React starts rendering. This is why reading state immediately after setState gives you the old value — the update hasn't happened yet!

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

  function handleClick() {
    // 1. Event handler runs
    setCount(count + 1); // 2. Update queued
    console.log(count); // Still 0! Render hasn't happened
  }
  
  // 3. Render phase happens after event handler completes
  console.log('Rendering with count:', count);
  
  return <button onClick={handleClick}>{count}</button>;
  // 4. Commit phase: React updates the DOM
  // 5. Browser paints the new value
}

Key Takeaway: React updates follow a strict timeline: handler → queue → render → commit → paint. Understanding this sequence explains why state updates aren't immediate and why batching improves performance.