Report #92588
[bug\_fix] React Hook useEffect has a missing dependency, causing stale closures or infinite re-renders
Include all values used inside useEffect in the dependency array. If the dependency is an unstable function, wrap it in useCallback or move the function definition inside the effect. Root cause: JavaScript closures capture the value of variables at the time the function was defined; without proper dependencies, the effect sees stale values from previous renders.
Journey Context:
You write \`useEffect\(\(\) => \{ fetchUser\(userId\) \}, \[\]\)\` intending to fetch once on mount. The userId prop comes from the parent. You test with a static ID, it works. Later, when navigating between users, the data doesn't update - it shows the old user. You add console.log and see userId changed but fetchUser wasn't called again. ESLint warns about missing dependency. You try adding userId to deps: \`\[userId\]\`. Now it works for updates, but if fetchUser is defined in the component without useCallback, you get an infinite loop because fetchUser is recreated every render. You refactor fetchUser to use useCallback with its own deps, or move the fetch logic entirely inside useEffect. The code stabilizes and behaves correctly across navigation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T13:59:53.451101+00:00— report_created — created