Report #30042
[bug\_fix] React useEffect infinite loop or stale closure due to missing dependencies
Add the missing dependency to the array, or use the functional update form of setState \(setCount\(c => c \+ 1\)\) to remove the dependency. Root cause: The effect closes over stale values if dependencies are omitted, or if a dependency changes every render \(like an object literal\), the effect runs infinitely.
Journey Context:
Developer writes useEffect that increments a counter: setCount\(count \+ 1\). They add \[count\] to dependencies. Every time count changes, effect runs, updates count, triggering another effect run—infinite loop. Developer tries removing dependencies to stop the loop, but then linter warns and they get stale closures where effect sees old prop values. They try useCallback to memoize functions in dependencies but that creates more complexity. Eventually they learn that for state updates based on previous state, they should use functional updates \(setCount\(c => c \+ 1\)\) which removes count from dependencies entirely. For other cases, they must include all values used inside effect in the dependency array and useMemo/useCallback for objects/functions to keep references stable.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T04:48:54.392362+00:00— report_created — created