Bug: Passing an array as a `useEffect` dependency causes an infinite loop
See original GitHub issueReact version: 16.13.0
Steps To Reproduce
When the following code is run in a React component, it causes an endless loop. Passing an array as a dependency to useEffect
(in this case, args
), causes that to happen, but it shouldn’t.
I’ve read that I can use [args.length]
to stop this from happening, but then the ESLint rule react-hooks/exhaustive-deps
throws an error, so I want to avoid doing it that way.
import React, { useEffect, useState } from 'react';
export default function Home() {
const args = ['a'];
const [value, setValue] = useState(['b']);
useEffect(() => {
setValue(['c']);
}, [args]);
console.log('value', value);
}
Notice in the code above, that I don’t even use or modify args
at all, in the useEffect
callback.
If I change the value of args
to a string, like const args = 'a'
, then there is no endless loop. So the problem seems to occur when the dependency is an array.
The current behavior
The page runs in an infinite loop.
The expected behavior
It should only run once.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7
Top Results From Across the Web
How to solve the React useEffect Hook's infinite loop patterns
Passing no dependencies in a dependency array. If your useEffect function does not contain any dependencies, an infinite loop will occur. For ...
Read more >useEffect dependency array causing infinite loop [duplicate]
I'm getting the following warning in the console: Line 19:6: React Hook useEffect has a missing dependency: 'data'. Either include it or remove ......
Read more >How to Solve the Infinite Loop of React.useEffect()
1.1 Fixing dependencies ... The infinite loop is fixed by correct management of the useEffect(callback, dependencies) dependencies argument.
Read more >Preventing infinite re-renders when using useEffect and ...
Changing state will always cause a re-render. By default, useEffect always runs after render has run. This means if you don't include a...
Read more >How to solve Infinity loop in React's useEffect - CodingDeft.Com
The useEffect hook runs again as we did not pass any dependency array to it and causes an infinite loop. To fix this,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t know what your business want to do, but as long as the outer layer of FC is OK. You don’t need to write useRef. This is purely a personal habit. If you write according to you, you basically initialize an array object every time, so the arg every time is different, resulting in an infinite loop.
Actually you can also write like this:
I got around this by adding the stringified version of the array as dependency instead.
This came in handy when having that dependency inside custom hooks
I guess this works because it’s an array of strings, however if that would be an array of objects, I probably would then pass a reference as mentioned before.