-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add RTKQ infinite query example app #4744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add RTKQ infinite query example app #4744
Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
cd4e48c
to
ee81aae
Compare
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 242ac2a:
|
0f1db3d
into
feature/infinite-query-integration
* Add blank RTK app * Nuke initial examples * Add react-router * Basic routing * Add msw * Configure MSW * Set up MSW * Use RTK CSB CI build * Add baseApi * Add pagination example * Add react-intersection-observer * Add infinite scroll example * Add max-pages example * Drop local example lockfile * Drop back to Vite 5 to fix TS issue * Align Vite versions to fix test TS error
* Add blank RTK app * Nuke initial examples * Add react-router * Basic routing * Add msw * Configure MSW * Set up MSW * Use RTK CSB CI build * Add baseApi * Add pagination example * Add react-intersection-observer * Add infinite scroll example * Add max-pages example * Drop local example lockfile * Drop back to Vite 5 to fix TS issue * Align Vite versions to fix test TS error
* Add blank RTK app * Nuke initial examples * Add react-router * Basic routing * Add msw * Configure MSW * Set up MSW * Use RTK CSB CI build * Add baseApi * Add pagination example * Add react-intersection-observer * Add infinite scroll example * Add max-pages example * Drop local example lockfile * Drop back to Vite 5 to fix TS issue * Align Vite versions to fix test TS error
Continued messing around with my RTKQ + Tanstack Virtual example with filters. Added filters to both the official Tanstack Query + Virtual example and my RTKQ "port" of the example. There are two meaningful differences in behavior that I am noticing. I intentionally added a 2 second artificial delay to the fetches so fetching behavior is easier to observe. Last Page Fetch When Filter Change RTKQ: When I check and uncheck the "show pinned" filter, if I immediately scroll to the bottom of the list, all of the cached pages refetch first. Once all of these old pages finish refetching, the new page (one at the bottom of list) will then fetch. This can result in a pretty high wait time to see the new page (2s delay * number of old pages that are getting refetched). See :27 timestamp in RTKQ video. Side point: It's probably ideal for fetching of new page to always take priority, even in situations where scroll position gets reset to top of a list with a bunch of cached pages and user immediately scrolls down to the very bottom. Copy of All Pages In Cache Get Appended To End In RTKQ After Filter Change Happens and Fetch New Page Finishes Video Screenshots RTKQ.Behavior.mp4Tanstack.Behavior.mp4Reproduction Repository https://github.com/agusterodin/rtkq-infinite-query-playground Side note: When originally playing around with these examples, I created really nice abstractions to clean things up (eg: move logic out of the component into hooks). I ditched those abstractions for now so that it's easier do a more 1-to-1 comparison between RTKQ and Tanstack behavior. I will most definitely re-improve the abstractions in my example and post to discussion board in the future :) |
@agusterodin : gotcha, thanks for putting this together! Just to check, what is supposed to be triggering the refetch? Skimmed the code and didn't see anything obvious. Hmm. Is it maybe the |
It is probably easier to understand what is happening in this hook I abstracted out. The only reason I got rid of this hook in my latest commit was so the code was closer to the official example. Basically when the elements that virtualizer renders on DOM change ( Will play around with |
At a glance it appears to behave much more ideally when I set |
Okay, good, that's what I was hoping :) Is there a reason you needed to have that turned on? |
Mostly so that we can keep cache around for a long period of time (high We keep the global setting on, but occasionally disable it on a per-hook basis if we're dealing with a situation where we fetch large payloads that don't change. Eg: we have a dashboard that fetches 10mb+ of data from an endpoint and we don't have to worry about the dashboard "results" changing after processing finishes. |
Gotcha. Yeah, having it "refetch before fetching the next page" in this example sounds like what I would expect - you changed the args, and doing that is configured to trigger a refetch, so it does that immediately and that has to complete before a next page can be fetched. So, sounds like "working as intended" in this case. The duplicate data does seem like a bug :) I'll have to see if I can repro that. |
It appears the Tanstack example does perform the refetch on mount / arg change but is always prioritizes the newest page before going back and refetching the old ones. For the particular use-case of a virtualized list, this seems ideal. Otherwise you could end up waiting a while before finally being able to see the new page. I will dig through Tanstack documentation to see if this is something they let you customize. Unsure of unintended consequences in Tanstack’s behavior though. |
* Add blank RTK app * Nuke initial examples * Add react-router * Basic routing * Add msw * Configure MSW * Set up MSW * Use RTK CSB CI build * Add baseApi * Add pagination example * Add react-intersection-observer * Add infinite scroll example * Add max-pages example * Drop local example lockfile * Drop back to Vite 5 to fix TS issue * Align Vite versions to fix test TS error
This PR:
Note that the "Pagination" example was originally a normal
useQuery
with apage
arg. I started porting it and then ran into issues figuring out how to track the current page. Per Dominik, this is actually a bad use case for an infinite query in the first place ( https://bsky.app/profile/tkdodo.eu/post/3lcbjhjvlpk24 ):I'm keeping it in here for now just because I've already got it written.
In the process I found a couple more features we're missing:
hasNext/PrevPage
either