ReactError Fix2026 Updated
Too Many Re-renders React— Fixed [Complete Guide 2026]
Also covers: Maximum update depth exceeded · infinite loop · useEffect loop
January 20266 min readReact 18 · Next.js
⚡ Two Most Common Causes
component.jsx
// ❌ Cause 1 — calling function instead of passing it
<button onClick={handleClick()}> // called on render!
<button onClick={handleClick}> // ✅ passed as reference
// ❌ Cause 2 — setState in render body
function Component() {
const [n, setN] = useState(0)
setN(n + 1) // infinite loop!
return <div>{n}</div>
}What Causes Too Many Re-renders?
Error
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
at renderWithHooks (react-dom.development.js:14985)
# or:
Error: Maximum update depth exceeded. This can happen when a component
calls setState inside useEffect, but useEffect either doesn't have a
dependency array, or one of the dependencies changes on every render.React caps re-renders at 25 per cycle. When this limit is hit, it means state is being updated on every render — creating a loop. The error message tells you the component but not the exact cause — you need to find the state update that fires unconditionally.
11 min
setState Called Directly in Render
State updated unconditionally in component bodycomponent.jsx
// ❌ WRONG — setState in render body = infinite loop
function Counter() {
const [count, setCount] = useState(0)
setCount(count + 1) // runs on every render → triggers another render
return <div>{count}</div>
}
// ✅ CORRECT — setState only in event handler
function Counter() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(c => c + 1)}>{count}</button>
}
// ✅ CORRECT — setState in useEffect with condition
function Counter({ initialValue }) {
const [count, setCount] = useState(0)
useEffect(() => {
setCount(initialValue) // runs once on mount
}, []) // empty deps = run once
return <div>{count}</div>
}21 min
Calling Event Handler Instead of Passing It
onClick={fn()} instead of onClick={fn}component.jsx
// ❌ WRONG — () calls the function immediately during render
<button onClick={handleClick()}>Click</button>
<input onChange={handleChange()} />
// ✅ CORRECT — pass the function reference, don't call it
<button onClick={handleClick}>Click</button>
<input onChange={handleChange} />
// ✅ If you need to pass arguments — use arrow function wrapper
<button onClick={() => handleDelete(item.id)}>Delete</button>
<input onChange={(e) => handleChange(e.target.value)} />33 min
Infinite useEffect Loop
useEffect updating its own dependencycomponent.jsx
// ❌ WRONG — data is in deps, effect sets data → infinite loop
const [data, setData] = useState([])
useEffect(() => {
fetchData().then(result => setData(result))
}, [data]) // ← data changes → effect runs → sets data → repeat
// ✅ CORRECT — empty deps, run once on mount
useEffect(() => {
fetchData().then(result => setData(result))
}, [])
// ✅ CORRECT — depend on ID, not the data itself
useEffect(() => {
fetchData(userId).then(result => setData(result))
}, [userId]) // re-run only when userId changesA safe rule: never put a state variable in a useEffect dependency array if that same effect sets that state. The effect will always cause a re-run.
43 min
Object/Array in Dependency Array
New object reference on every rendercomponent.jsx
// ❌ WRONG — {} creates new reference every render
const options = { page: 1, limit: 10 } // new object each render!
useEffect(() => { fetchData(options) }, [options]) // infinite loop
// ✅ CORRECT — useMemo stabilises the reference
const options = useMemo(() => ({ page: 1, limit: 10 }), [])
// ✅ CORRECT — use primitive values as deps instead
useEffect(() => {
fetchData({ page, limit })
}, [page, limit]) // primitives compare by value — stable
// ✅ CORRECT — move object outside component if it's static
const OPTIONS = { page: 1, limit: 10 } // defined once, never changes
function MyComponent() {
useEffect(() => { fetchData(OPTIONS) }, [])
}53 min
useMemo and useCallback to Prevent Re-renders
Memoize expensive computations and stable callbacksParentComponent.jsx
import { useState, useMemo, useCallback, memo } from 'react'
const ChildList = memo(function ChildList({ items, onDelete }) {
console.log('ChildList rendered')
return (
<ul>
{items.map(i => (
<li key={i.id}>
<button onClick={() => onDelete(i.id)}>Delete</button>
</li>
))}
</ul>
)
})
export default function ParentComponent() {
const [items, setItems] = useState([...])
const [filter, setFilter] = useState('')
// ✅ useMemo — only recalculates when items or filter changes
const filteredItems = useMemo(
() => items.filter(i => i.name.includes(filter)),
[items, filter]
)
// ✅ useCallback — stable reference, won't cause child re-render
const handleDelete = useCallback((id) => {
setItems(prev => prev.filter(i => i.id !== id))
}, []) // no deps — setItems is stable
return <ChildList items={filteredItems} onDelete={handleDelete} />
}Prevention
- Never call setState unconditionally in the component render body
- Pass event handlers as references: onClick={fn} not onClick={fn()}
- Be specific with useEffect deps — primitives over objects
- Stabilise objects with useMemo before using them as useEffect deps
- Use React.memo + useCallback to prevent unnecessary child re-renders
- Add React DevTools Profiler to spot which component is re-rendering frequently
Frequently Asked Questions
What causes 'too many re-renders' in React?+−
React throws this error when a component re-renders more than the allowed limit (currently 25). The most common causes: calling setState directly during render (not inside an event handler or effect), calling an event handler function in JSX instead of passing it, or a useEffect that updates state in its dependency array causing infinite loops.
How do I fix 'Maximum update depth exceeded' in React?+−
Find where setState is being called on every render. Check: 1) Is setState called directly in the component body (not in useEffect or event handler)? 2) Are you writing onClick={handleClick()} instead of onClick={handleClick}? 3) Does your useEffect update a state that is in its own dependency array?
useEffect causing infinite loop — how to fix?+−
Your useEffect's dependency array includes a value that the effect itself changes. Solution: remove the state variable from the dependency array if the effect does not need to re-run when it changes, or use a ref instead, or restructure the effect to not update the dependency.
Object in useEffect dependency array causing infinite loop?+−
Objects and arrays are compared by reference in JavaScript. A new object literal {} is never equal to another {} even with the same contents. Memoize with useMemo: const config = useMemo(() => ({ key: value }), [value]). Or extract the specific primitive values from the object as dependencies.
How do I stop a child component from causing re-renders?+−
Wrap the child component in React.memo: export default React.memo(ChildComponent). This prevents re-renders when parent re-renders but props have not changed. Also wrap callback props in useCallback to preserve reference equality.
React.memo not preventing re-renders — why?+−
React.memo does a shallow comparison. If you pass objects, arrays, or functions as props, they are new references on each parent render. Memoize them: wrap function props with useCallback, wrap object/array props with useMemo, or move them outside the component if they are static.
Need Expert Help?
We Build Production-Grade React Apps
Softplix builds full-stack React and Next.js applications. Dealing with performance or rendering issues? We can help.
Talk to an Engineer