Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React stops with 'Too many re-renders' as soon as a component loads

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

Short answer

This error means a component sets state while it is rendering, every time it renders, so React stops the loop. The usual culprit is an event handler that is called instead of passed, like onClick={save()}. The other is a set call in the component body with no condition around it.

  • Applies to React 19, Next.js 15, Next.js 16
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the browser console, expand the error and read the stack. The first frame inside your own file names the line that calls a state setter during render.

    # In the browser console: click the arrow next to the error,
    # then look for the first frame in your own component file.
    # To find suspicious handlers quickly:
    grep -rnE "on[A-Z][A-Za-z]*=\{[A-Za-z.]+\([^)]*\)\}" app components src
  2. 2Match what you see, then jump to its fix

    1. 1The stack ends in the component's return statement, and JSX contains onClick={handleClick()}An event handler is called during render instead of passedFix
    2. 2The stack points at a line like setCount(...) or dispatch(...) sitting directly inside the component function, outside any handler or EffectA state setter runs in the component body with no conditionFix

What does the error look like?

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

Cause 1 of 2

An event handler is called during render instead of passed

1Check

Look at every handler prop in the component. A pair of parentheses after the function name runs it while React builds the element, and if that function sets state, every render triggers another render.

grep -rnE "on(Click|Change|Submit)=\{[A-Za-z.]+\(" app components src

2Fix

Pass the function itself, or wrap the call in an arrow function so it runs only when the event fires.

// wrong: runs during render
<button onClick={handleClick()}>Save</button>

// right: passes the handler
<button onClick={handleClick}>Save</button>

// right: inline function, also fine for passing arguments
<button onClick={() => handleClick(item.id)}>Save</button>

3Verify

Reload the page. The component should render once and the console error should be gone; clicking the button should now run the handler.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 2 of 2

A state setter runs in the component body with no condition

1Check

Search the component body for set calls that are not inside an event handler. Each such call schedules a render, and the next render calls it again.

grep -nE "^\s*set[A-Z][A-Za-z]*\(" components/MyComponent.tsx

2Fix

Move the call into an event handler or an Effect. If you really need to adjust state while rendering (for example, to track the previous value of a prop), guard it with a condition that becomes false after the first update.

const [prevCount, setPrevCount] = useState(count)
const [trend, setTrend] = useState(null)

if (prevCount !== count) {
  setPrevCount(count)
  setTrend(count > prevCount ? 'increasing' : 'decreasing')
}

3Verify

Reload and watch the console. The page should render normally and the state should settle after the first update.

npm run dev

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Read handler props as a habit: a function name with no parentheses means pass it, with parentheses means run it now.
  • Keep state updates inside handlers and Effects; treat the component body as a place that only calculates JSX.
  • Turn on the react-hooks ESLint rules so conditional or misplaced hook usage is flagged in the editor.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

next/link throws React.Children.only on its children

Error: React.Children.only expected to receive a single React element child.

React.Children.only accepts exactly one React element and throws for anything else, including an array with one item.

3 causes, each with a fix

Web apps

Hydration failed after adding next-themes dark mode

Console reports a server and client mismatch on the html element

next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.