Course contentsShow
← React
Lesson 601 of 2,39616. useEffect for Data FetchingFree lesson

Why React Does This: Finding Missing Cleanup

Double-running exposes effects that don't properly clean up, like forgotten AbortController or lingering subscriptions.

Why React Does This: Finding Missing Cleanup

You'll learn: Why React intentionally double-runs effects in development to help you catch cleanup bugs before production.

The Purpose Behind the "Annoyance"

When you see your effect run twice in StrictMode, it's not a bug—it's a stress test. React is deliberately simulating what happens when components mount, unmount, and remount in real user scenarios (like navigating back in a single-page app, or Suspense boundaries re-rendering).

By mounting, immediately unmounting, then mounting again, React forces your cleanup function to run. If you forgot to write cleanup (or wrote it incorrectly), you'll see the problem immediately in development:

  • Doubled event listeners firing twice per interaction
  • Multiple timers stacking up
  • Lingering subscriptions never getting canceled
  • Zombie fetch requests updating unmounted components

The Real-World Analogy

Think of it like a smoke alarm test. You press the button to make sure it would work in a real fire. React "presses the button" on your effects to make sure cleanup would work when users actually navigate around your app.

Without this double-run, you might ship code where:

  • Users navigate to a page, leave, come back, and suddenly everything fires twice
  • AbortControllers never abort, wasting bandwidth
  • Memory leaks accumulate over time

StrictMode catches these issues before users do.

Why This Matters Now

Since you've learned about AbortController, ignore flags, and cleanup patterns, you understand what to clean up. StrictMode ensures you actually did it. If your effect behaves correctly after double-running in dev, it'll behave correctly in production navigation scenarios.

Key Takeaway: StrictMode's double-run is a free QA tool that exposes missing or broken cleanup logic—embrace it as early bug detection, not a nuisance.