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.

Infinite loop in useEffect using blank object or array

See original GitHub issue

Using React Version: 16.8.4 When using blank object or array as a setter for function inside useEffect does cause infinite loop. However using string or number does not trigger this. Here’s the minimal example:

function Example() {
  const [count, setCount] = useState(0);
  const [foo, setFoo] = useState({});

  useEffect(() => {
     console.log("Infinite Loop");
     setCount(100);    // doesn't cause infinite loop.
     // but this does
     // setFoo({});   // or setFoo([]);
  });

  return <div />;
}

I don’t know if it’s a bug or default behaviour because in earlier version of react (in v16.7.0-alpha.0), both string and number also causing infinite loop which seems to be fixed in latest one.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
mlandalvcommented, Mar 13, 2019

It seems to behave according to the docs.

If you update a State Hook to the same value as the current state, React will bail out without rendering the children or firing effects.

Both {} and [] creates a new object or array and hence triggers an update. 100 is primitive and doesn’t.

1reaction
gaearoncommented, Mar 13, 2019

Re: example in https://github.com/facebook/react/issues/15096#issuecomment-472349151, you can fix it by adding the dependencies argument. (In this case, it’s [] because your effect doesn’t depend on any values from render scope except setCurrentUser which is guaranteed to be stable.)

     function Example() {
        const [currentUser, setCurrentUser] = useState({});
        useEffect(() => {
              const unsub = firebase.auth().onAuthStateChanged(user => {
                  unsub();
                  if(user) setCurrentUser(user);
              });
        }, []) // <--------- I added this
       return <div/>
     }

Note that you should only do it when it’s safe — i.e. when you don’t use any props/state and values derived from them in the effect, and you also don’t call any functions that use those.

See:

Hope this helps.

For a deeper dive:

Read more comments on GitHub >

github_iconTop Results From Across the Web

Infinite loop in useEffect - reactjs - Stack Overflow
An empty array at the end of a useEffect is a purposeful implementation by the developers to stop infinite loops in situations where...
Read more >
How to solve the React useEffect Hook's infinite loop patterns
To get rid of your infinite loop, simply use an empty dependency array like so: const [count, setCount] = useState ...
Read more >
5 useEffect Infinite Loop Patterns | by Naveen DA
useEffect uses shallow object comparison to determine, whether the data was changed or not. Due to weird JavaScript conditional systems ....
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 >
React useEffect infinite loop despite empty array-Reactjs
Related Query · React useEffect infinite loop despite empty array · React Redux state array variable pass as prop to child component, either...
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