# 警告：检测到过时闭包。传递给 useCallback 的回调引用了一个可能已过时的变量。

- **ID:** `react/use-callback-stale-closure`
- **领域:** react
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

useCallback 钩子在其闭包中捕获了一个未在依赖数组中列出的变量，导致回调引用了过时的值。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| React 18.2.0 | active | — | — |
| React 19.0.0-rc.1 | active | — | — |

## 解决方案

1. ```
   Add the missing variable to the dependency array of useCallback. For example: const handleClick = useCallback(() => { console.log(count); }, [count]);
   ```
2. ```
   Use a ref to store the variable and access it inside the callback without adding it to dependencies. Example: const countRef = useRef(count); countRef.current = count; const handleClick = useCallback(() => { console.log(countRef.current); }, []);
   ```
3. ```
   Upgrade to React 19 and use the new use hook which automatically tracks dependencies.
   ```

## 无效尝试

- **** — This ignores the stale closure issue and may still reference outdated values, leading to bugs. (70% 失败率)
- **** — useMemo is for values, not callbacks, and does not solve the dependency tracking issue. (80% 失败率)
- **** — This forces the callback to always be stale, breaking any logic that relies on current state. (90% 失败率)
