Why Your Effect Runs Twice on Mount
What you'll learn: Why React intentionally runs your effects twice during development, and what this reveals about your code's resilience.
If you've opened your console and seen your useEffect fire twice when a component first mounts—even with an empty dependency array—you're not going crazy. This is StrictMode doing its job.
The Intentional Double-Run
In development, React's StrictMode deliberately simulates an unmount/remount cycle for every component. Here's the sequence:
- Component mounts
- Effect runs
- Effect cleanup runs (even though the component didn't actually unmount)
- Effect runs again
This happens only in development. Production builds run effects normally (once per mount with [] dependencies).
Why React Does This
StrictMode is stress-testing your effects. It's asking: "What if this component unmounts and remounts later?" This happens in real apps when:
- Users navigate away and back
- Suspense boundaries trigger
- Fast Refresh during development
- Parent components re-render with different keys
If your effect breaks when run twice, it means you're missing cleanup logic—and you'll hit bugs in production eventually.
What This Reveals
A well-written effect should be resilient to re-mounting. If running twice causes:
- Duplicate subscriptions → You need cleanup
- Double fetch requests → You need AbortController or ignore flags
- Timer issues → You need to clear timers in cleanup
The double-run isn't a bug—it's an early warning system catching your bugs before users do.
Key Takeaway: StrictMode's double-run in development simulates unmount/remount to expose missing cleanup logic—effects that break when run twice will eventually break in production too.