Can we prevent useInfiniteQuery { refetchOnMount: 'always' } from refetching all cached pages? #3576
Replies: 10 comments 39 replies
-
if there are 20 pages in the cache, 20 pages will be refetched. why? because anything else could lead to duplicate and/or missing data. suppose one entry on page one was deleted in the meantime, and we refetch only page 1. That means the last entry on page 1 will be the same entry on page 2, unless we refetch that as well. all imperative refetch methods let you provide a
but yeah, you'll run into the above mentioned problems. alternatively, you can just remove all pages from the cache except the first page, when you unmount all observers. You would likely need to tie this to your component lifecycle, and you can remove it by calling but honestly, all this is not needed if you just let the refetches happen. What's the problem with those in the first place? |
Beta Was this translation helpful? Give feedback.
-
have similar issue ... I think this issue hurt the performance , Imagine you are fetching 100 pages ... and for next visit react query re-fetch all 100 pages at once :( Is there any solution to prevent this behavior? |
Beta Was this translation helpful? Give feedback.
-
such rookie mistake/decision by react query. Consistency > Performance. In real world everything focus on performance, how does it even make sense for RQ developer that prefetching 100+ pages is better than inconsistency is beyond me. API calls are always extremely expensive. Go to any big companies like Google or Amazon and tell them consistency better than performance they will boot you out of their doors lol. |
Beta Was this translation helpful? Give feedback.
-
Any clear solution to disable this behavior ? all things works good in the pagination , but this refetch by useInfiniteQuery causes a lot of unnecessary API calls |
Beta Was this translation helpful? Give feedback.
-
Great! thanks for your confirmation and your help! I really appreaciate!! :) |
Beta Was this translation helpful? Give feedback.
-
Sorry for disturb you again. I tried now and is not working. Here you have a GIF: |
Beta Was this translation helpful? Give feedback.
-
I will try to replicate in codesandbox but i'm working with React Native. |
Beta Was this translation helpful? Give feedback.
-
Hi again @TkDodo, Is not working in React Native. Here you can see my code until can create a Expo version: https://github.com/SoyDiego/react-query-infinit-scroll/blob/master/TheSimpsons.tsx Thanks for your time |
Beta Was this translation helpful? Give feedback.
-
My use case is like this:
|
Beta Was this translation helpful? Give feedback.
-
@TkDodo I agree with the comments above
You definitely need to think about this Currently I need to use custom hook with |
Beta Was this translation helpful? Give feedback.
-
Hello,
I ran into an interesting problem today which I couldn't really figure out a way to solve. So, dropping a question here.
I have a post feed rendered using
useInfiniteQuery
and I have set the option{ refetchOnMount: 'always'}
as I want this page to reload the feed when the user navigates into the feed page.But the problem is that react-query refetches all the pages previously cached/loaded even before the user scrolls for more. As a result, if the user had loaded 20 pages of data previously, RQ would refetch all 20 pages on mount. Is there a way to prevent this behavior?
Would love to learn if any of you encountered this problem or have solved it as well.
Thank you,
Prasanna
Beta Was this translation helpful? Give feedback.
All reactions