Lazy fetching of data will cause infinite loop / runaway effect
See original GitHub issueDescribe 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:
- Created 4 years ago
- Reactions:6
- Comments:22 (8 by maintainers)
Top 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 >
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 would expect
refetch
to be referentially transparent the same way as in:setIsValid
is always the same reference. I would not considerReact.useEffect(() => refetch(), [refetch])
as main use case, but referential transparency affects how react components re-render.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.