Too many re-renders in React - Fix Infinite Loop (2026) | Tutorials Logic
What is This Error?
The "Too many re-renders" error occurs when React detects an infinite rendering loop. This happens when a component keeps triggering state updates during rendering, causing React to re-render endlessly until it hits its limit and throws this error.
Error Message:
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
Common Causes
Quick Fix (TL;DR)
// ❌ Problem — calling setState during render
function App() {
const [count, setCount] = useState(0);
setCount(count + 1); // Infinite loop!
return {count};
}
// ✅ Solution — only call setState in event handlers or useEffect
function App() {
const [count, setCount] = useState(0);
return (
);
}
Common Scenarios & Solutions
Scenario 1: Immediately Invoked Event Handler
The most common mistake — calling the function immediately instead of passing a reference.
// ❌ setCount(0) is called immediately during render!
// ❌ handleClick() is called immediately during render!
// ✅ Pass a function reference, not a call
// ✅ Pass the function reference directly
Scenario 2: useEffect Without Dependency Array
// ❌ Runs after every render → sets state → triggers render → infinite loop
useEffect(() => {
setData(fetchData()); // No dependency array!
});
// ✅ Empty array = run only once on mount
useEffect(() => {
setData(fetchData());
}, []); // Add dependency array
// ✅ Run only when specific value changes
useEffect(() => {
setData(fetchData(userId));
}, [userId]); // Only re-run when userId changes
Scenario 3: Object/Array in useEffect Dependencies
// ❌ options is a new object on every render → infinite loop
const options = { page: 1, limit: 10 };
useEffect(() => {
fetchData(options);
setResults(data);
}, [options]); // options changes every render!
// ✅ Use primitive values as dependencies
const [page, setPage] = useState(1);
const limit = 10;
useEffect(() => {
fetchData({ page, limit });
setResults(data);
}, [page]); // Primitive value — stable reference
// ✅ Or memoize the object
const options = useMemo(() => ({ page: 1, limit: 10 }), []);
Scenario 4: setState in Render Body
function Counter() {
const [count, setCount] = useState(0);
setCount(count + 1); // ❌ Called during render!
return {count};
}
function Counter() {
const [count, setCount] = useState(0);
// ✅ Only update state in response to events
return (
);
}
Best Practices to Avoid This Error
Related Errors
Key Takeaways
- "Too many re-renders" means React detected an infinite rendering loop
- Never call setState directly in the render body — only in event handlers or useEffect
- Pass function references to event handlers: onClick={fn} not onClick={fn()}
- Always add a dependency array to useEffect to control when it runs
- Objects and arrays in dependency arrays cause infinite loops — use primitives instead
- Use React DevTools Profiler to identify which component is causing excessive re-renders
Frequently Asked Questions
Level Up Your React js Skills
Master React js with these hand-picked resources
10,000+ learners
Free forever
Updated 2026
Related React Topics