[v5] useInfiniteQuery - how do I get feature parity with the new changes in v5 for my use case? #5921
-
Hi @TkDodo ! This question is more directed at you since you've been the one handling all the breaking changes lately :D First and foremost here is a quick explanation and a demo of what I'm currently doing with
Since I want data to be up to date fairly frequently, I'm currently using boring standard polling, by hand, to refetch new rows every 20s. Now, let's focus on the use case where data is sorted from oldest to most recent events and see how the table behaves: Screen.Recording.2023-08-28.at.16.31.53.movOnce the user is at the latest known page so far, I will keep polling for a new data starting from the previous cursor. newCoachingEventsQueryRefetch<
UseNewCoachingEventsReturnedData['pages'][number]
>({
refetchPage(lastPage, index, allPages) {
return (
index === allPages.length - 1 &&
Re.isNil(lastPage.serverModelPageInfo.nextCursorRow)
)
},
}) I only need to fetch the last page because the previous events are immutable. This way I can spare some API calls. Here is the query itself: return useInfiniteQuery({
queryKey: newVehicleCoachingEventsQueryKey(params),
queryFn: (metaData) => {
return fetchNewCoachingEvents({
serverModel: ...,
})
},
getNextPageParam: (lastPage): NewVehicleCoachingEventsQueryPageParam => {
if (Re.isNil(lastPage.serverModelPageInfo.nextCursorRow)) {
// Return undefined to tell react-query there is no next page available
return undefined
}
const { nextCursorRow } = lastPage.serverModelPageInfo
return {
nextCursorRow,
pageSize: params.serverModel.pageSize,
}
},
}) What I can do with v5Since I don't want to refetch every single page I've loaded so far, to my knowledge the best option would be to use Now, I understand this can be very useful for mobile apps where you don't actually display a scrollbar of what has been loaded, like we do in desktop apps. Let's say I use
Bottom lineI don't see how I can keep the same user experience by using the new Thank you for your time 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Thanks for sharing your use-case. I think it's quite unique, and doing the same thing in v5 will require some changes on your end, but I think it's possible:
of course, the check for Let me know if that makes sense and if that'll solve your situation. |
Beta Was this translation helpful? Give feedback.
-
@TkDodo it worked well and I managed to keep every previous behavior! Thank you once again 👍 |
Beta Was this translation helpful? Give feedback.
Thanks for sharing your use-case. I think it's quite unique, and doing the same thing in v5 will require some changes on your end, but I think it's possible:
I'd use
refetchInterval
instead of manually callingrefetch
. If you only want it to be enabled on the last page, you can pass a function to it, like:refetchInterval: (data) =>
and then compute the interval. If you are not on the last known page, just returnfalse
or0
, and if you are on the last known page, return the number (in milliseconds) for your interval.I'd just let react-query refetch all the pages, and implement an opt-out mechanism in the queryFn. This can look something like this (pseudo code):