Too many re-renders

React ErrorState LoopMediumLast updated: June 28, 2026Tested on:React v19.0Next.js 16June 2026

The component enters an infinite loop of updating its state during the render phase.

Too many re-renders Quick Fix⏱️ Est. Fix Time: 2–5 minutes

Usually happens because:

  • Direct function invocation inside onClick listener
  • State setter called unconditionally in component body
  • Circular dependency in useEffect

🔍 Quick Checklist:

📊 Where this usually happens (estimated occurrence)

Direct event handler invocation70%
Unconditional state sets in body20%
useEffect dependency loops10%

What is Too many re-renders?

This error occurs when a React component executes a state update function (like `setCount` or `setOpen`) directly inside its main render body. In React, updating a state value tells the framework to schedule a new render cycle for that component. When this update is called unconditionally during rendering, it triggers another render, which triggers another update, resulting in an infinite loop. To prevent browser memory exhaustion, React halts execution and throws this error.

Common Causes

  • Invoking State Setters in JSX Event Props: Passing direct function invocations to event props (e.g., `onClick={setCount(count + 1)}`) instead of passing function references (e.g., `onClick={() => setCount(count + 1)}`).
  • Direct State Mutation in Component Bodies: Executing state updates unconditionally inside the main function body or render block of a component.
  • Unconditional State Changes inside render paths: Changing state based on conditions checked during the render phase.

Common Mistakes

  • Confusing function invocation (`onClick={setOpen(true)}`) with callback declaration (`onClick={() => setOpen(true)}`).
  • Updating parent states unconditionally inside child components during mounting phases.

How to Fix

1Wrap event-triggered state setters in arrow functions: Always ensure event props receive function references rather than immediate execution results.
2Compute values inline during render: If a value depends on state, calculate it dynamically in the component body instead of saving it back to state.
3Move body-level updates into useEffect: Safely handle side-effects and initial configuration checks after mounting.

Framework-Specific Examples

Calling a state setter directly in the function body updates state on every render, triggering an infinite loop.

Direct body state update Example
import { useState } from 'react';

function BadProfile({ user }) {
  const [name, setName] = useState(user.name);
  
  // Wrong: re-setting name on every render
  setName(user.name);
  
  return <h1>{name}</h1>;
}

Best Practices

  • Always check your event handlers for trailing parentheses (`()`) inside JSX tags.
  • Ensure state update calls inside `useEffect` are guarded by dependency conditions.

Frequently Asked Questions (FAQ)

Q: Why does React limit the number of renders?

React limits nested render updates (typically up to 50) to catch infinite loops early. This prevents the page from freezing or crashing the user's browser.

Q: Can I call a state setter in useEffect?

Yes, but you must supply a dependency array. If you update a state variable that is also listed as a dependency in the same useEffect, it can cause an infinite loop. Ensure the update is guarded by a conditional check.

Q: What is the difference between onClick={setCount} and onClick={setCount()}?

`onClick={setCount}` passes the function reference to be called when the user clicks the button. `onClick={setCount()}` invokes the function immediately during rendering, setting state and causing a loop.

Q: Does updating a ref trigger re-renders?

No. Mutating `ref.current` does not trigger a re-render, so updating refs in the component body will not cause this error.

Still having this problem?

Didn't solve your problem?

SuggestRequest Error