# 错误：超过最大更新深度。这可能发生在组件在 useEffect 内调用 setState 而没有依赖数组时

- **ID:** `react/maximum-update-depth-exceeded-useeffect`
- **领域:** react
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

useEffect 调用 setState 时未指定依赖项，导致渲染 → 副作用 → setState → 渲染的无限循环。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| React 16.8+ | active | — | — |
| React 17 | active | — | — |
| React 18 | active | — | — |
| React 19 | active | — | — |

## 解决方案

1. ```
   Add a dependency array to useEffect with the values that should trigger the effect:

useEffect(() => {
  setCount(count + 1);
}, [count]); // Now it only runs when count changes, avoiding infinite loop if count is not updated in a way that causes re-render
   ```
2. ```
   Use the functional update form of setState and an empty dependency array if the effect should run only once:

useEffect(() => {
  setCount(prev => prev + 1);
}, []); // Runs once on mount
   ```
3. ```
   If the effect depends on props or state that change, use useReducer to manage complex state transitions and avoid direct setState in effects.
   ```

## 无效尝试

- **** — Does not address the root cause; the useEffect still runs without dependencies, triggering setState on every render. (80% 失败率)
- **** — Causes an infinite loop during render because setState triggers a re-render immediately. (90% 失败率)
- **** — The condition may still be true on every render, or the effect runs on every render anyway, leading to the same loop. (70% 失败率)
