跪拜 Guibai
← All articles
React.js

React's useEffect: The Sync Mechanism, the StrictMode Double-Run, and Why Infinite Loops Happen

By Gitrocr ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A wrong mental model of useEffect produces infinite loops, memory leaks, and stale data in production. The double-invoke in StrictMode is a deliberate bug detector, not something to work around; treating effects as synchronization rather than lifecycle callbacks removes most of these failures.

Summary

React's useEffect synchronizes component state with the outside world. It runs after the browser paints, and its cleanup function runs before the next effect or on unmount. The mental model that matters: think of it as declaring which state and props an effect depends on, not as a lifecycle replacement.

The dependency array decides when effects fire. No array means every render; an empty array means once on mount; a populated array runs when any listed value changes. StrictMode in development runs mount effects twice (run, cleanup, run) to expose improper cleanup; production builds run once, so there is nothing to fix beyond writing idempotent effects and clean cleanup.

Common failures come from reference equality: objects, arrays, and functions get new identities each render, so putting them in dependencies causes infinite loops without useMemo or useCallback. Stale closures, missing dependencies, and heavy synchronous work are the other traps. The practical fixes are one effect per concern, complete dependency arrays, race condition handling with a cancelled flag or AbortController, and extracting reusable logic into custom hooks.

Takeaways
— useEffect runs after the browser paint, not during render.
— Cleanup runs before the next effect execution and on component unmount.
— No dependency array causes the effect to run after every render.
— An empty dependency array runs the effect once on mount only.
— A populated dependency array runs on mount and whenever any listed value changes.
— StrictMode in development runs mount effects twice (setup, cleanup, setup) to expose cleanup bugs; production runs once.
— Objects, arrays, and functions in dependencies are new references each render, causing infinite loops unless stabilized with useMemo or useCallback.
— Race conditions in async effects need a cancelled flag or AbortController.
— Functional updates or useRef prevent stale closures.
— Each effect should handle one concern; reuse logic via custom hooks.
Conclusions

The dependency array is only as reliable as the identity of its values; object and function literals break it every render, which is why StrictMode's remount is so effective at exposing bad assumptions.

StrictMode's intentional double-run is a test harness for cleanup logic, not a production behavior. Code that survives it runs with confidence in production, though newcomers often waste time trying to suppress it.

Splitting effects by responsibility turns the dependency array into a declaration of granular needs, making the synchronization model explicit and cutting down on the cognitive load of tracking which state an effect reads.

Concepts & terms
Dependency array
The second argument to useEffect. An empty array runs the effect once on mount; a populated array runs it on mount and whenever any listed value changes; omitting it runs after every render.
Cleanup function
A function returned from the effect callback that runs before the next effect execution and on unmount, used to remove event listeners, cancel timers, or abort requests.
Stale closure
When an effect captures state values from an old render because its dependency array didn't include them, leading to out-of-date data. Fixed with functional updates or useRef.
StrictMode double-invoke
In development, React intentionally runs mount effects twice (setup, cleanup, setup) to surface improper cleanup code and side effects that aren't idempotent.
From the discussion
Featured comments
组合搭配函数

The fact that effects run twice under StrictMode really confuses novices the first time they encounter it; pointing it out in advance is necessary. The dependency array and cleanup function are two pitfalls that you won't forget after stepping on them once.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗