react · JavaScript
The message
Too many re-renders. React limits the number of renders to prevent an infinite loop.
What it means
A render set state, that state triggered another render, and React cut the loop to stop an infinite one. Nearly always this is a function being called where it should have been passed: onClick={setOpen(true)} runs on every render, and it has to be onClick={() => setOpen(true)}. The other common shapes are setting state directly in the body of the render, and a useEffect whose dependency list contains the value it updates. Move state changes into an event handler or a useEffect, and if the value can simply be computed during render, ask whether it needs to be state at all.
The fix
There is no one-line command for this. The explanation says what to look at instead.
- Printed by
- react
- JavaScript
- 15
Browser and Node errors usually tell you what broke and not why the value became what it was — you read undefined, it was not a function, it was not JSON — so the place to fix is upstream of the line that threw, and silencing just that line with ?. and default values makes the same problem reappear further away in a form that is harder to recognise.
Reading an error message
- Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
- If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
- Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
- The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
- Before pasting a fix, check what it throws away. Some of these cannot be undone.
Common questions
Q. What does “Too many re-renders. React limits the number of renders to prevent an infinite loop.” mean?
A render set state, that state triggered another render, and React cut the loop to stop an infinite one. Nearly always this is a function being called where it should have been passed: onClick={setOpen(true)} runs on every render, and it has to be onClick={() => setOpen(true)}. The other common shapes are setting state directly in the body of the render, and a useEffect whose dependency list contains the value it updates. Move state changes into an event handler or a useEffect, and if the value can simply be computed during render, ask whether it needs to be state at all.
Q. How do I fix it?
There is no one-line command. The explanation above says what to look at instead.
Q. Which tool prints this?
react. It sits under JavaScript, and the message runs to 14 words.