# Warning: A stale closure was detected. The callback passed to useEffect references a variable that may be outdated.

- **ID:** `react/stale-closure-useeffect`
- **Domain:** react
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

A useEffect callback captures a variable from an earlier render, and when that variable changes, the effect still uses the old value because the dependency array is missing or incomplete.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| React 16.8.0 | active | — | — |
| React 17.0.2 | active | — | — |
| React 18.2.0 | active | — | — |

## Workarounds

1. **Add all variables referenced inside the effect to the dependency array: `useEffect(() => { console.log(count); }, [count]);`** (90% success)
   ```
   Add all variables referenced inside the effect to the dependency array: `useEffect(() => { console.log(count); }, [count]);`
   ```
2. **Use the functional update form of setState or useReducer if the stale variable is a state value: `setCount(prev => prev + 1)`** (85% success)
   ```
   Use the functional update form of setState or useReducer if the stale variable is a state value: `setCount(prev => prev + 1)`
   ```
3. **Use `useRef` to hold the latest value and update it via an effect: `const countRef = useRef(count); countRef.current = count;` then access `countRef.current` inside the effect.** (80% success)
   ```
   Use `useRef` to hold the latest value and update it via an effect: `const countRef = useRef(count); countRef.current = count;` then access `countRef.current` inside the effect.
   ```

## Dead Ends

- **** — The effect never re-runs when the captured variable changes, so the closure remains stale indefinitely. (80% fail)
- **** — The variable is still captured at the time of effect creation; if it changes, the effect still uses the old value. (75% fail)
- **** — If the ref is not manually updated when the variable changes, it still holds the old value. (60% fail)
