# Error: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect without a dependency array

- **ID:** `react/maximum-update-depth-exceeded-useeffect`
- **Domain:** react
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

A useEffect calls setState without specifying dependencies, causing an infinite loop of render → effect → setState → render.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| React 16.8+ | active | — | — |
| React 17 | active | — | — |
| React 18 | active | — | — |
| React 19 | active | — | — |

## Workarounds

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** (95% success)
   ```
   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** (90% success)
   ```
   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.** (85% success)
   ```
   If the effect depends on props or state that change, use useReducer to manage complex state transitions and avoid direct setState in effects.
   ```

## Dead Ends

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