Why Effects Run After Paint
What you'll learn: How React keeps your UI fast and responsive by running effects after the browser has painted.
The Sequence Matters
When a component updates, React follows a precise order:
- Render — React calculates what changed
- Commit — React updates the DOM
- Paint — The browser paints pixels to the screen
- Effect — Your
useEffectcode runs
This sequence is deliberate. By waiting until after paint, React ensures the user sees visual updates immediately.
Why Not Run Effects Earlier?
Imagine if your effect ran before paint:
useEffect(() => {
// Heavy computation or API call
const result = expensiveCalculation();
setData(result);
});
If this blocking code ran before the browser painted, the user would see a frozen, unresponsive UI. The button they clicked wouldn't visually respond. Forms would feel sluggish.
The Restaurant Analogy
Think of a restaurant:
- Render/Commit/Paint = Taking your order, cooking it, and serving it to your table
- Effect = The waiter quietly refilling water glasses after your food arrives
You don't want to wait for water refills before getting your meal. Similarly, users shouldn't wait for side effects before seeing UI updates.
Keeping Interactions Smooth
By running effects after paint, React prioritizes what users see and feel. Click feedback is instant. Text appears immediately. The effect work (fetching data, updating subscriptions, logging analytics) happens in the background without blocking the main interaction.
This is why effects are called "effects" — they're the side consequences that follow the primary goal of rendering UI.
Key Takeaway: Effects run after paint to keep the UI responsive and prevent side-effect code from blocking visual updates that users need to see immediately.