Report #50244
[bug\_fix] React Hook useEffect has a missing dependency
Include all reactive values \(props, state, functions\) used inside useEffect in the dependency array, or extract static functions outside component/useCallback
Journey Context:
Developer writes useEffect\(\(\) => \{ fetchData\(userId\) \}, \[\]\) intending to run once on mount. When userId prop changes later, the effect still uses the old userId from the initial closure, causing stale data fetch or missing updates. ESLint react-hooks/exhaustive-deps warns about missing dependency. Developer tries adding userId to deps but now effect runs on every render \(if userId is unstable\) or creates infinite loop if setState is called inside. Debugging reveals the function fetchData was recreated every render, causing new effect runs. Final fix involves either adding userId to deps array \(correcting the stale closure\) while wrapping fetchData in useCallback with its own deps, or moving fetchData definition inside the effect to make it self-contained. This ensures effect sees current props without excessive re-runs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T14:48:51.224432+00:00— report_created — created