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.

Lazy fetching of data will cause infinite loop / runaway effect

See original GitHub issue

Describe the bug With a lazy useGet, placing the refetch within an effect will cause an infinite loop of fetches due to the refetch function getting regenerated on each render.

To Reproduce Steps to reproduce the behavior:

  const { refetch } = useGet(`/some-endpoint`, { lazy: true });

  React.useEffect(() => {
    refetch();
  }, [refetch]);

Expected behavior I believe that refetch should be memoised and should not fail the dependency check in an effect. SO that you can safely refetch data in an effect with the refetch function as a dependency.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
stereoboostercommented, Nov 25, 2019

I would expect refetch to be referentially transparent the same way as in:

const [isValid, setIsValid] = useState(true);

setIsValid is always the same reference. I would not consider React.useEffect(() => refetch(), [refetch]) as main use case, but referential transparency affects how react components re-render.

<ExpensiveComponent refetch={refetch}>
3reactions
johncblandiicommented, Sep 24, 2020

I started to wrap the component and have the reason why it was lazy loading in a different component and then let the query run as expected, but I couldn’t help fight to figure out a solution so we didn’t have to create extra components across the app.

For now I think calling it out on the readme would be great. I went there to see if there was a note about effects.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to solve the React useEffect Hook's infinite loop patterns
Solve the issue of infinite loops when using the useEffect Hook in React to more smoothly utilize the Hook for your app's side...
Read more >
PL - Lecture #27 - Programming Languages
Typing Data; Judgments for recursive types; “Runaway” instances ... to be well founded: that we'd never get stuck in some kind of type-level...
Read more >
Does human brain have examples of hanged state or infinite ...
The brain has dysfunctional and degenerate states that might be analogous to a computer hanging or becoming stuck in an infinite loop.
Read more >
Effects of Too Much Lazy Loading on Web Performance
Using lazy loading for such applications can slow down the scrolling experience since we need to wait until the data loads. This will...
Read more >
What are the biggest issues you see with React in its current ...
It's far too easy to work yourself into infinite loops with hooks. ... When I started working with React the data agnostic nature...
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