Skip to content
FR
  1. LuzineDebug
  2. JavaScript & Node.js
  3. Error: Too many re-renders. React limits the num…
JavaScript & Node.js Beginner ≈ 10 min to fix

Fix "Too many re-renders. React limits the number of renders" in React

Error message
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
  • Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
  • Error: Minified React error #301; visit https://react.dev/errors/301 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
  • Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.
  • Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
In short

A component updates its state during its own render, which triggers a new render, which updates the state, and so on. The most common cause is an event handler that is called instead of passed: write onClick={() => setCount(count + 1)}, not onClick={setCount(count + 1)}.

Be the first to confirm this fix

What this error means

Calling a set function from useState schedules a new render. If that call happens directly in the component body, every render triggers another one, forever. React detects this loop (beyond about 25 consecutive renders) and throws "Too many re-renders" to avoid freezing the browser; in production, the message becomes "Minified React error #301". A similar loop exists with useEffect: an effect that updates state without a dependency array, or with a dependency recreated on every render, causes "Maximum update depth exceeded" (error #185). The faulty component is named in the stack trace.

Common causes

  1. Function called instead of passed: onClick={setOpen(true)} runs setOpen on every render instead of waiting for the click.
  2. setState in the component body: for example to copy a prop into state or compute a derived value.
  3. Effect without dependencies that updates state: useEffect(() => setItems(...)) without an array runs after every render.
  4. Unstable dependency: an object, array or function created during render is new every time, so the effect that depends on it re-runs endlessly.

Step-by-step fix

  1. Find the faulty component

    The console says "The above error occurred in the <ComponentName> component". In production, reproduce in development mode to get the full message instead of error #301.

  2. Pass a function to event handlers

    A handler must be a function that React will call later. Wrap the call in an arrow function whenever there are arguments.

    javascript
    // Wrong: runs during render
    <button onClick={setCount(count + 1)}>+1</button>
    
    // Right: runs on click
    <button onClick={() => setCount(count + 1)}>+1</button>
  3. Compute derived values instead of storing them

    A value computed from props or state does not need its own state: compute it during render, with useMemo if the computation is expensive.

    javascript
    // Wrong
    const [total, setTotal] = useState(0);
    setTotal(items.reduce((sum, i) => sum + i.price, 0));
    
    // Right
    const total = items.reduce((sum, i) => sum + i.price, 0);
  4. Give useEffect a dependency array

    An effect that updates state must list its dependencies, so it only runs when they change. With [], it only runs on mount.

    javascript
    useEffect(() => {
      fetch(`/api/users/${userId}`)
        .then((res) => res.json())
        .then(setUser);
    }, [userId]);
  5. Stabilize object and function dependencies

    Use primitive values as dependencies, or memoize the object with useMemo and the function with useCallback, so they only change when needed.

    javascript
    const filters = useMemo(() => ({ status, page }), [status, page]);
    
    useEffect(() => {
      loadOrders(filters);
    }, [filters]);
  6. Reset state when a prop changes

    Instead of calling setState during render when a prop changes, give the component a key: React recreates it with fresh state.

    javascript
    <ProfileForm key={user.id} user={user} />

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Enable the eslint-plugin-react-hooks ESLint plugin: the exhaustive-deps rule flags missing or unstable dependencies.
  • Only keep in state what cannot be computed from props or other state.
  • Check every onClick, onChange or onSubmit: the value must be a function, not the result of a call.

Frequently asked questions

What is the difference between "Too many re-renders" and "Maximum update depth exceeded"?

"Too many re-renders" comes from a setState called during render. "Maximum update depth exceeded" comes from chained updates in an effect or lifecycle method. Both are infinite state update loops.

Why does my useEffect run twice in development?

In Strict Mode, React 18 and 19 mount, unmount and remount every component in development to detect effects that are not cleaned up. It is not an infinite loop and does not happen in production.

How do I pass an argument to an onClick without an infinite loop?

Wrap the call in an arrow function: onClick={() => removeItem(item.id)}. The function only runs on click.

Useful tools for this error

More JavaScript & Node.js errors

All JavaScript & Node.js errors →