Report #57582
[bug\_fix] Warning: React Hook useEffect has a missing dependency: 'someFunction'. Either include it or remove the dependency array. OR Stale closure bug where state inside useEffect shows old value despite state update elsewhere.
Include all reactive values \(props, state, functions\) used inside useEffect in the dependency array. If including a function causes infinite loops, wrap it in useCallback or define it inside the effect. For state that depends on previous state, use the functional update form setState\(prev => ...\). Root cause: JavaScript closures capture the value of variables at the time the function is defined. If dependencies aren't listed, React doesn't re-run the effect when values change, causing the effect to use stale values from previous renders.
Journey Context:
You write a component that fetches data based on a userId prop. You write \`useEffect\(\(\) => \{ fetchUser\(userId\); \}, \[\]\)\` intending to fetch only on mount. When the parent updates userId, the component still displays the old user data. You add \`userId\` to the dependency array \`\[userId\]\`, which fixes the staleness but now the effect runs on every render because \`fetchUser\` is also a dependency and it's redefined on every render. You try to add \`fetchUser\` to deps, causing an infinite loop. You then learn about moving the function inside the effect or using useCallback to memoize it. The journey involves understanding that the dependency array is not just for 'when to run' but for 'what values the closure needs to be fresh'. The established fix is to include all reactive values and use useCallback/useMemo for functions/objects to stabilize references.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T03:08:33.939535+00:00— report_created — created