Report #36186
[bug\_fix] React Hook useEffect has a missing dependency: 'someVariable'. Either include it or remove the dependency array, or the effect runs with stale props/state causing infinite loops or outdated data.
Include all reactive values \(props, state, functions\) used inside the useEffect in the dependency array. If including a function causes infinite loops, wrap it in useCallback or move it inside the effect. If including an object causes loops, use a primitive value or memoize the object. Root cause: JavaScript closures capture the value at the time of render; without the dependency, the effect sees stale values from previous renders.
Journey Context:
You write a component that fetches data when a userId prop changes. You write useEffect\(\(\) => \{ fetchUser\(userId\); \}, \[\]\) to mimic componentDidMount. ESLint warns about missing dependency 'userId', but you ignore it thinking you only want it to run once on mount. Later, when navigating between user profiles using client-side routing, the component doesn't refetch data because the effect never re-runs, showing the previous user's data. You realize the closure captured the initial userId. You add userId to the deps array, but now every keystroke in an unrelated input \(that updates state\) causes a refetch because you defined fetchUser outside the effect and it's a new function each render. You finally move the fetch logic inside the effect, or wrap fetchUser in useCallback with its own dependencies, creating a stable dependency array that only changes when userId actually changes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T15:13:11.284707+00:00— report_created — created