Too many re-renders. React limits the number of renders to prevent an infinite loop
The React error "Too many re-renders. React limits the number of renders to prevent an infinite loop" happens when you have reached an infinite render loop. This may be due to several reasons.
- Changing the state in the main body of the component.
- Invoking an event handler, instead of passing as a function.
Let us discuss this in detail with example codes.
Changing the state in the main body of the component
Changing the state in the component body as in the below example will throw an error “Too many re-renders. React limits the number of renders to prevent an infinite loop”.
import { useState } from 'react';
function App() {
const [name, setName] = useState("");
setName("John"); // Throws React Error: Too many re-renders
return (
<div>{name}</div>
);
}
export default App;
The above code will throw an error because we are trying to change the name setName("John");
in the main body of the component. That means the function setName("John");
will be executed at the time of each component rendering. The function setName("John");
changes the value in the state name
.
When a state was changed, the component will again be rendered. So, the function setName
changes the state which leads to another re-render. This will go infinitely and throws the error.
This error was solved in the below example code by using useEffect hook.
import { useState, useEffect } from 'react';
function App() {
const [name, setName] = useState("");
useEffect(()=>{
setName("John");
}, [])
return (
<div>{name}</div>
);
}
export default App;
One possible solution is to use useEffect
hook and put the function setName
inside it. An empty array is given as a dependency for useEffect
. So setName("John");
will only be executed at the time of initial rendering and not executed on re-rendering.
Invoking an event handler, instead of passing as a function
Let's take an example of the below code which throws React Error: Too many re-renders.
// This code throws Error: Too many re-renders
import { useState } from 'react';
function App() {
const [count, setCount] = useState(0);
return (
<div>
<label>Count: {count}</label>
<button onClick={setCount(count + 1)}>Increment</button>
</div>
);
}
export default App;
In the above code, the error comes from inside the onClick event handler of the button.
<button onClick={setCount(count + 1)}>Increment</button>
At the time of the component initial rendering, setCount(count + 1)
will be executed and it changes the state count
which leads to component re-render. It will go an infinite loop.
To prevent this, we have to pass setCount
as a callback function like () => setCount(count + 1)
.
A full code example is given below.
import { useState } from 'react';
function App() {
const [count, setCount] = useState(0);
return (
<div>
<label>Count: {count}</label>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
export default App;
Conclusion
The React error "Too many re-renders. React limits the number of renders to prevent an infinite loop" happens due to the state update in the main body of the component or invoking an event handler, instead of passing as a function.