Introduction

The “Too many re-renders” error in React is one of the most common runtime issues developers face when working with functional components and hooks. It usually occurs when a component enters an infinite render loop, causing React to stop execution to prevent crashing the application.

If not handled correctly, this error can break your UI, degrade performance, and make debugging difficult.

In this article, you will learn:

What is “Too many re-renders” Error?

This error occurs when a React component continuously updates its state during rendering, causing an infinite loop.

React detects this and throws an error:

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

How React Rendering Works

Understanding this is key:

  1. Component renders

  2. State or props change

  3. Component re-renders

If state updates happen during rendering, the cycle repeats infinitely.

Root Causes of the Error

1. Updating State Directly Inside Component Body

function App() {
  const [count, setCount] = useState(0);
  setCount(count + 1);
  return <div>{count}</div>;
}

Problem

State updates inside the render phase trigger infinite re-renders.

Fix

Move logic inside useEffect:

useEffect(() => {
  setCount(count + 1);
}, []);

2. Incorrect Event Handler Usage

<button onClick={setCount(count + 1)}>Click</button>

Problem

Function executes immediately during render.

Fix

<button onClick={() => setCount(count + 1)}>Click</button>

3. Missing Dependency Array in useEffect

useEffect(() => {
  setCount(count + 1);
});

Problem

Effect runs on every render → infinite loop.

Fix

useEffect(() => {
  setCount(count + 1);
}, []);

4. Updating State Based on State Without Condition

useEffect(() => {
  setCount(count + 1);
}, [count]);

Problem

Each update triggers another update.

Fix

Add condition:

useEffect(() => {
  if (count < 5) {
    setCount(count + 1);
  }
}, [count]);

Real-World Scenario

Scenario: API Call Loop

useEffect(() => {
  fetchData();
  setData(...);
}, [data]);

Issue

Solution

Before vs After Fix

Before:

After:

Common Patterns That Cause Re-renders

CauseIssueSolution
State update in renderInfinite loopMove to useEffect
Immediate function callExecutes on renderUse arrow function
Missing dependenciesRepeated executionAdd dependency array

Debugging Tips

Advantages of Fixing Re-render Issues

Disadvantages (If Ignored)

Best Practices

Summary

The “Too many re-renders” error in React occurs when a component enters an infinite rendering loop due to improper state updates or incorrect hook usage. By understanding how React’s rendering lifecycle works and applying best practices like using useEffect correctly, handling event handlers properly, and controlling state updates, developers can prevent this error and build stable, high-performance React applications.