Why Effects Can't Run During Render
You'll learn why side effects must wait until after React finishes rendering, not happen in the middle of it.
The Render Phase Must Stay Pure
Remember from earlier lessons that the Render Phase is a pure calculation. React calls your component function to figure out what the UI should look like, but it doesn't change anything yet. This purity is critical because React might call your component multiple times (like in Strict Mode) or even discard the result without committing it.
If you tried to run side effects during render—like updating the DOM, making an HTTP request, or setting a timer—you'd break this purity. You'd be changing the outside world before React has even decided whether to use that render result.
The Problem with Mid-Render Effects
Think of rendering like an architect drawing blueprints. The architect calculates measurements, draws lines, and plans the structure—but doesn't actually start building. If the architect started hammering nails while still sketching, they'd create a mess if they needed to revise the plans.
Similarly, if effects ran during render:
- React couldn't safely retry renders
- Strict Mode's double-render would cause duplicate side effects
- The DOM might update before React is ready
- The UI would be inconsistent during the calculation phase
The Three-Phase Separation
This is why React has the Effects Phase that runs after the Commit Phase completes. By the time effects run, React has already applied all DOM changes and the browser has painted. Your effects see the final, committed state of the world—not some in-progress snapshot.
Key Takeaway: Effects can't run during render because render must be a pure, repeatable calculation. Side effects wait until the Effects Phase to ensure consistency and give React full control over when things actually change.