Professional Documents
Culture Documents
When the infinite loop is running without any stop, eventually browser will kill the tab that
your code is running. So don’t use Infinite Loop without any breaking point.
useEffect
useEffect hook allows us to perform side effects in a component. When hooks were
introduced the react 16, the useEffect hook is gain more traction than any other hooks.
Because it provides combined functionalities of componentDidMount,
componentDidUpdateand componentWillUnmount life cycle methods.
useEffect hook triggers the callback function, only if the dependencies were changed. And
it uses shallow comparison to compare the values of the hook.
You can consider the useEffect as a power stone, it is a most powerful stone and if you
haven’t handled it properly the stone would destroy you.
Without dependency
useEffect without dependencies generally consider a bad practice, so always try to avoid it.
What’s happening?
If the useEffect is triggering the callback only when the dependencies are changes, why we
ended with an infinite loop here.
You need to take into account important another mantra of React which is “When state or
Props is changing, the component will re-render”.
In this code, we set the state value using setData on-network call success, it will trigger
the component re-render. Since the useEffect doesn’t have value to compare, so it will call
the callback.
And Again Fetch will trigger setData and setData will trigger, the component re-render
and so on.
How to fix this issue?
We need to specify the dependencies as the empty array.
Function as dependency
useEffect uses shallow object comparison to determine, whether the data was changed or
not. Due to weird JavaScript conditional systems .
When you run this code, it will throw Maximum update depth exceeded which means the
code having an infinite loop.
What’s happening?
Since useEffect uses the shallow comparison for the compare the values. The shallow
comparison for the function will always give false.
Array as dependency
As you might know, the shallow comparison for two is always false, so passing
dependencies as an array will also lead to Infinite Loop
When you run this code, the browser console will throw this error.
What’s happening?
The shallow comparison of two arrays is always false, so useEffect will always trigger the
callback.
Objects as dependency
You might guess the shallow comparison of two objects is always false, so the useEffect
will trigger the callback always.
When you run this code, your browser console will be thrown an infinity loop error.
Wrong dependency
Wrong dependencies are nothing to do with React not even javascript. When the wrong
dependencies are used, we must take the blame.
I hope there is no need to explain this issue pattern and its fixes. If you want the
explanation and fixes, please let me know in the comment.
Note: There is plenty of ways to avoid infinite loop inside a React component, I mentioned
only a few ways.
blog.tomilkieway.com