Prefetching infinite query triggers infinite re-renders #3866
-
Describe the bugI was trying to implement an infinite scroller using But, prefetching infinite query leads to infinite rendering. Is there a way to achieve the above? Your minimal, reproducible examplehttps://codesandbox.io/s/black-hill-4g2n7y?file=/src/List.js Steps to reproduce
Expected behaviorThe next route is prefetched and when the user clicks on the loadMore button, the data is returned from cache. How often does this bug happen?Every time Screenshots or VideosNo response Platform
react-query version4.0.0 TypeScript versionNo response Additional contextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
without an exit condition, this will of course trigger an infinite render, because every fetch triggers onSuccess, which triggers another fetch, which triggers onSuccess, ... the question is when you do you want to fetch the next page? There is no prefetching once a query is displayed because fetching the next page will just display it as well. what we do in the examples is use an intersection observer and fetch the next page when this element gets into the viewport. This can be at the very end of the list, or e.g. 5 entries before that. That way, when the user scrolls to the "almost bottom", the fetch will happen, and by the time they are at the bottom, they will see the new entries. |
Beta Was this translation helpful? Give feedback.
without an exit condition, this will of course trigger an infinite render, because every fetch triggers onSuccess, which triggers another fetch, which triggers onSuccess, ...
the question is when you do you want to fetch the next page? There is no prefetching once a query is displayed because fetching the next page will just display it as well.
what we do in the examples is use an intersection observer and fetch the next page when this element gets into the viewport. This can be at the very end of the list, or e.g. 5 entries before that. That way, when the user scrolls to the "almost bottom", the fetch will happen, and by the time they are at the bottom, they will see the new entries.