Report #48952
[bug\_fix] React Hook useEffect has a missing dependency: 'someVar'. Either include it or remove the dependency array
Include all reactive values \(props, state, variables derived from them, and functions defined in component scope\) used inside useEffect in the dependency array. If this causes an infinite loop, wrap the function in \`useCallback\` or move it inside the effect.
Journey Context:
Developer is building a search filter in a Next.js e-commerce site using React 18. They write \`useEffect\(\(\) => \{ fetchProducts\(query, sortOrder\); \}, \[query\]\);\` but \`sortOrder\` is also used inside the effect. The ESLint plugin \`react-hooks/exhaustive-deps\` warns about missing \`sortOrder\`, but the developer ignores it. Later, when \`sortOrder\` changes, the effect doesn't re-run, leaving stale results. Conversely, if they add \`fetchProducts\` to the deps \(which is defined in the component\), they get an infinite loop because \`fetchProducts\` is recreated every render. The developer spends hours debugging why the UI flickers or doesn't update, adding \`console.log\` everywhere and checking if the API is caching responses incorrectly. They try using \`useRef\` to store previous values, making the code convoluted. The rabbit hole reveals that JavaScript closures capture the value of variables at the time the function is created. Without the correct dependencies, the effect closure sees stale values from the initial render. The fix works because React's \`exhaustive-deps\` ESLint rule enforces that every reactive value referenced inside the effect must be declared in the dependencies. This ensures the effect re-runs when those values change, receiving fresh closures. For functions, using \`useCallback\` to memoize the function ensures its reference is stable across renders, preventing the infinite loop while still satisfying the dependency array requirements.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T12:39:06.445628+00:00— report_created — created