Report #10109
[bug\_fix] React Hook useEffect has a missing dependency or causes stale closure/infinite loop
Include all reactive values \(props, state, functions\) in the dependency array. If functions cause infinite loops, memoize them with useCallback or define them inside useEffect. Root cause: JavaScript closures capture lexical scope at creation; omitting dependencies causes effects to reference stale values, while including unmemoized functions triggers re-runs every render.
Journey Context:
Developer writes useEffect to fetch data when userId changes, including \[userId\] in deps. Inside, they call handleFetch\(userId\), defined outside. ESLint warns handleFetch is missing from deps. Developer ignores it. When userId changes, the effect runs but handleFetch closes over stale state from the previous render, showing outdated UI. Later, they try adding handleFetch to deps, causing an infinite loop because handleFetch is redefined every render. The 'aha' moment comes when they realize handleFetch must be wrapped in useCallback with its own deps, or defined inside the effect to capture fresh values without adding to the array.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T09:50:11.846424+00:00— report_created — created