Report #37050
[bug\_fix] React Hook useEffect has a missing dependency \(ESLint warning\) or infinite loop / stale closure
Add the missing variable to the useEffect dependency array. If the dependency is a function defined outside the effect, either define it inside the effect or wrap it with \`useCallback\`. If it's an object/array that changes every render, memoize it with \`useMemo\` or move it inside the effect. Root cause: JavaScript closures capture the lexical scope at definition time. If a variable used in the effect isn't in the dependency array, the effect sees the value from the render when the effect was first created \(stale closure\), leading to bugs. Conversely, unstable object references cause effects to run infinitely.
Journey Context:
Developer writes \`useEffect\(\(\) => \{ fetchUser\(userId\) \}, \[\]\)\` intending to fetch once on mount. Later adds logic inside to check \`if \(currentUser \!== user\)\` and update state. ESLint warns about missing \`userId\` and \`currentUser\` dependencies. Developer ignores ESLint to 'optimize' performance. User navigates to a different profile page; \`userId\` prop changes but the effect never re-runs, showing stale data from previous user. Developer then adds \`userId\` to deps but \`fetchUser\` is defined in the component body, causing a new function reference every render, triggering an infinite loop of fetch calls and \`Too many re-renders\` error. Eventually solves by wrapping \`fetchUser\` in \`useCallback\` with its own dependencies, or by moving the fetch logic entirely inside the useEffect.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T16:39:43.667070+00:00— report_created — created