5 Jul 2021
3 ways to cause an infinite loop in React
Have you spent some time trying to debug an infinite loop in React? Maybe you've hung your browser a couple of times in the process. Or encountered one of these ๐
Here are 3 potential causes of infinite loops in React.
I. Updating the state inside the render
If you update the state directly inside your render method or the body of a functional component, it will cause an infinite loop.
State updates โ triggers re-render โ state updates โ triggers re-render โ and so on...
Fix ๐
If you want to update a state only once when the component mounts, use useEffect
with an empty array as a dependency.
II. Infinite loop in useEffect
If you keep updating a state inside useEffect
with a property you update set as a dependency, it will cause an infinite loop.
count
updates โ useEffect
detects updated dependency โ count
updates โ useEffect
detects updated dependency โ and so on...
Fix ๐
If you want to update a state based on its previous value, use a functional update. This way, you can remove the state property from the dependency list and prevent an infinite loop.
III. Incorrectly set event handlers
This is not the right way to set event handlers. You need to provide a function to onClick
, not the result of the function execution. By executing a function before setting a handler, you update the state inside the render, which causes an infinite loop.
State updates โ triggers re-render โ state updates โ triggers re-render โ and so on...
Fix ๐
Set a function to the onClick
event. This is the proper way to set event handlers. This way, the state will only update after a button click and won't cause an infinite loop.
How to spot infinite loops
Every time you update a state, picture the sequence of events that will happen after the update. If, without additional user interaction, this sequence leads you back to the same state update, you likely have an infinite loop.