---
title: "Too many re-renders. React limits the number of renders to prevent an infinite loop"
description: "This React error means a component sets state while rendering, which triggers another render forever. See the usual causes, like calling a handler instead of passing it, and how to fix them."
url: https://www.danny.engineering/answer/react/too-many-re-renders-react-limits-the-number-of-renders-to-prevent-an-infinite-loop
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["React"]
---

# Too many re-renders. React limits the number of renders to prevent an infinite loop

## The short answer


Something in your component is updating state during render. Each update causes another render, which updates state again, so React stops the loop and throws. Most often you are calling a function in JSX instead of passing it. Wrap it in an arrow function so it only runs on click.


```diff
- <button onClick={setCount(count + 1)}>Add</button>
+ <button onClick={() => setCount(count + 1)}>Add</button>
```


## Why it happens


A React component is a function that runs every time it renders. Anything you write in the function body, or inside the JSX it returns, runs on every render. A state setter such as `setCount` asks React to render again with the new value.


If a setter runs unconditionally during render, you get a cycle: render, set state, render, set state. React allows a small number of these updates during render, then throws this error rather than freezing the page.


The common causes are:

- Calling a handler in a prop: `onClick={handleClick()}` runs `handleClick` while rendering and passes its return value as the prop.
- Setting state directly in the component body, for example to copy a prop into state.
- Setting state inside a render-time calculation, such as inside a `.map` over items.

A similar error, "Maximum update depth exceeded", appears when the loop goes through an effect instead, for example a `useEffect` that sets state on every run without a dependency array.


## How to fix it


### Pass functions, do not call them


If the handler needs an argument, wrap it in an arrow function.


```typescript
function TodoList({ todos }: { todos: Todo[] }) {
  const [selected, setSelected] = useState<string | null>(null);

  return (
    <ul>
      {todos.map((todo) => (
        <li key={todo.id}>
          <button onClick={() => setSelected(todo.id)}>{todo.title}</button>
        </li>
      ))}
    </ul>
  );
}
```


### Derive values instead of storing them


If you are setting state from props or other state during render, you usually do not need state at all. Calculate the value as a normal variable.


```typescript
// Before: loops forever
function Basket({ items }: { items: Item[] }) {
  const [total, setTotal] = useState(0);
  setTotal(items.reduce((sum, item) => sum + item.price, 0));
  return <p>Total: {total}</p>;
}

// After: no state needed
function Basket({ items }: { items: Item[] }) {
  const total = items.reduce((sum, item) => sum + item.price, 0);
  return <p>Total: {total}</p>;
}
```


If the calculation is expensive, wrap it in `useMemo`. If you use the React Compiler, it can memoise this for you.


### Guard updates that must happen during render


Occasionally you do need to adjust state when a prop changes, such as resetting a selection when the list changes. React supports this if the update is behind a condition that stops it repeating.


```typescript
const [prevItems, setPrevItems] = useState(items);
if (items !== prevItems) {
  setPrevItems(items);
  setSelection(null);
}
```


Often a cleaner option is to give the component a `key` so React resets all its state when the key changes.


### Give effects the right dependencies


For the effect version of the loop, add a dependency array and make sure the effect does not set a value that is also one of its dependencies, unless it checks first.


## Common pitfalls

- Writing `onClick={handleClick(id)}` because it reads naturally. It runs immediately.
- Using an object or array literal as an effect dependency. It is a new value on every render, so the effect runs every time.
- Copying props into state with `useEffect` and then deriving more state from it. Each step adds another render.
- Fixing the symptom with a ref flag. Find the update that runs during render and move it to an event handler or derive the value instead.

## Related reading

- [React: You might not need an effect](https://react.dev/learn/you-might-not-need-an-effect)
- [React: Responding to events](https://react.dev/learn/responding-to-events)
