question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Passing an array as a `useEffect` dependency causes an infinite loop

See original GitHub issue

React 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:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
FuzzyFadecommented, Mar 7, 2020

@FuzzyFade Yeah, this works! So is this necessary for any non-string values passed to useEffect deps?

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:

import React, { useEffect, useState, useRef } from 'react';

export default function Home() {
  const [value, setValue] = useState(['b']);
  const {current:a} = useRef(['a'])
  useEffect(() => {
    setValue(['c']);
  }, [a])
}
0reactions
zanonacommented, Jul 25, 2022

I got around this by adding the stringified version of the array as dependency instead.

const a = ['a','b','c'];
useEffect(() => {...}, [a.toString]);

This came in handy when having that dependency inside custom hooks

const products = useProducts(['100-01', '100-02']);

function useProducts(skus) {
    const [products, setProducts] = useState([]);
    useEffect(() => fetch(...).then(setProducts) , [skus.toString()]);
    return products;
} 

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found