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

When this error occurs, your console will display the following error message πŸ‘‡

Why this error occurs ?

The error “Too many re-renders. React limits the number of renders to prevent an infinite loop” occurs for multiple reasons:

React has a mechanism to prevent infinite loops by limiting the number of renders that can occur in a row. By default, this limit is set to 50 renders. When the limit is exceeded, React throws the “Too many re-renders” error to prevent the browser from crashing.

Here are few common mistakes that can lead to this error:

1. If you update the state or props directly within the render function of a component, it can cause an infinite loop of rendering.

import {useState} from 'react';

export default function App() {
  const [count, setCount] = useState(0);
  
  // ⛔️ Too many re-renders. React limits the number
  // of renders to prevent an infinite loop.
  setCount(count + 1);
  
  return (
    <div>
      <h3>Count: {count}</h3>
    </div>
  );
}

2. Incorrect usage of hooks: In functional components using hooks, calling hooks like `useState` or useEffect` inappropriately, such as within loops or conditions, can trigger the error.

3. Having a `useEffect` hook that sets and re-renders infinitely.

import { useEffect, useState } from "react";
import "./App.css";

function App() {
  const [count, setCount] = useState(0);

// ⛔️ Too many re-renders. React limits the number
  // of renders to prevent an infinite loop.
  useEffect(() => {
    setCount(count + 1);
  });
  return (
    <div>
      <p>Count: {count}</p>
    </div>
  );
}

export default App;

4. Instead of passing a function as an event handler, you directly invoke the event handler immediately.

import {useState} from 'react';

export default function App() {
  const [count, setCount] = useState(0);

  // ⛔️ Too many re-renders. React limits the number
  // of renders to prevent an infinite loop.
  return (
    <div>
      <button onClick={setCount(count + 1)}>Increase</button>
      <h1>Count: {count}</h1>
    </div>
  );
}

How to fix the error ?

To resolve this error, you need to find the part of your code that is causing an infinite loop and change it to prevent the infinite loop.

Here are a few ways to help you fix the error:

1. Ensure that you’re not updating the state or props directly within the render function. Instead, perform these updates in response to events or in lifecycle methods like `componentDidUpdate` or `useEffect`.

import { useEffect, useState } from "react";
import "./App.css";

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

export default App;

2. Ensure that hooks are called unconditionally and at the top level of the functional component.

3. Ensure that state updates within lifecycle methods are conditional.

import { useEffect, useState } from "react";
import "./App.css";

function App() {
  const [count, setCount] = useState(0);
  useEffect(() => {
    // Correct: State update with a condition
    if (someCondition) {
      setCount(count + 1);
    }
  }, [count]);
  return (
    <div>
      <p>Count: {count}</p>
    </div>
  );
}

export default App;

4. Instead of directly executing the event handler, pass the function to the onClick event.:

import { useState } from "react";

export default function App() {
  const [count, setCount] = useState(0);

  const handleClick = () => {
    setCount(count + 1);
  };

  return (
    <div>
      <button onClick={handleClick}>Increase</button>
      <h1>Count: {count}</h1>
    </div>
  );
}

I hope this article helped you to solve the error you were facing πŸ™

Leave a Comment