Report #81432
[bug\_fix] React Hook useEffect has a missing dependency: 'X'. Either include it or remove the dependency array \(or infinite re-render loop when fixing the warning\)
Include the missing dependency in the array. If the added dependency is an object or function that changes identity every render \(causing an infinite loop\), wrap that value in \`useMemo\` \(for objects/arrays\) or \`useCallback\` \(for functions\), or use a primitive dependency instead. Root cause: JavaScript closures capture the lexical environment at render time; missing dependencies cause 'stale closures' where the effect sees old values. Conversely, unstable object identities cause effects to run every render, potentially triggering state updates that cause loops.
Journey Context:
Developer writes \`useEffect\` to fetch user profile when \`userId\` changes: \`useEffect\(\(\) => \{ fetchProfile\(userId\) \}, \[\]\)\` leaving array empty to mimic 'componentDidMount'. ESLint warns 'missing dependency: userId'. Developer dismisses warning. Later, when \`userId\` prop changes from parent, the effect doesn't re-run, showing stale profile data \(stale closure\). Developer adds \`userId\` to dependency array \`\[userId\]\`, but now the effect runs continuously in an infinite loop because \`fetchProfile\` is defined inside the component and changes reference every render, or because the effect updates state that changes \`userId\`. Developer panics, adds \`fetchProfile\` to dependency array, making loop worse. After reading React docs, learns about dependency stability. Refactors to wrap \`fetchProfile\` in \`useCallback\` with its own dependencies, or moves \`fetchProfile\` outside component. Alternatively, uses a primitive dependency like \`\[userId, fetchProfileTrigger\]\` instead of object deps. Loop stops, data stays fresh. Developer learns to treat \`exhaustive-deps\` ESLint rule as mandatory and to think about referential equality in JavaScript.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T19:17:01.467243+00:00— report_created — created