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:
- Event handler runs — Your
onClickor other handler executes immediately - State updates queued — Calls to
setStateschedule updates, they don't apply yet - Render phase — React calculates what changed by calling your component functions
- Commit phase — React applies those changes to the actual DOM
- 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.