# React Hook useEffect缺少依赖项：'variable'。要么包含它，要么删除依赖项数组。

- **ID:** `react/missing-exhaustive-deps`
- **领域:** react
- **类别:** build_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

exhaustive-deps ESLint规则检测到在useEffect内部使用的变量未在其依赖项数组中列出，可能导致过时闭包或错过更新。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| eslint-plugin-react-hooks 4.6.0 | active | — | — |
| React 18.2.0 | active | — | — |

## 解决方案

1. ```
   Add the missing variable to the dependency array. If the variable is a function, wrap it in useCallback first.
   ```
2. ```
   If the variable is not meant to be reactive (e.g., a stable reference), use useRef to store it and access via .current.
   ```

## 无效尝试

- **Adding // eslint-disable-next-line react-hooks/exhaustive-deps without understanding the dependency** — Suppressing the warning does not fix the underlying stale closure bug; it only hides the lint error. (80% 失败率)
- **Removing the dependency array entirely ([] to undefined)** — Without a dependency array, the effect runs after every render, which can cause performance issues and infinite loops if it updates state. (70% 失败率)
- **Using useRef to store the variable and reading from ref inside the effect** — This works but is often unnecessary and can be confusing; it also bypasses the reactive nature of the variable. (40% 失败率)
