Replies: 2 comments 2 replies
-
all of this is up to you. https://codesandbox.io/s/github/tanstack/query/tree/main/examples/react/load-more-infinite-scroll |
Beta Was this translation helpful? Give feedback.
2 replies
-
For classical pagination just use useQuery |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm not sure how to use the
getPreviousPageParam
function, and I'm curious about its specific use cases?I couldn't find any examples online; it seems like everyone is only using
getNextPageParam
. Any guidance or some examples of how to usegetPreviousPageParam
, especially when implementing classic pagination or am I better of usinguseQuery
for classic pagination?Is the
getPreviousPageParam
mainly intended for use with APIs that support a previous cursor? I ask because I can also obtain the previous cursor from the fetched datadata.pages
array (see below). Are there any specific advantages to usinggetPreviousPageParam
in such cases?Also implementing support for a previous cursor in an API would require an additional database operation to scan backwards (in descending order), which would slow down the query. Given the additional cost of the second operation, wouldn't an offset-based API be a better option in this case?
I'm trying to implement classic page pagination so that I can store the pagination state in the URL (page and page size or cursor). I'm wondering if it's possible to achieve this using a cursor-based API with
useInfiniteQuery
andgetPreviousPageParam
, or if I should useuseQuery
instead. I'm starting to feel thatuseInfiniteQuery
and cursors may not be the right approach for classic pagination, especially if I want to store the state in the URL. Of course, if I make the API support a previous cursor, then I could store the state in the URL but data updates would cause additional trouble for the old cursor (the page number would not match with the old cursor).Example - Current test implementation
Currently, I'm implementing the classic pagination using
useInfiniteQuery
and thefetchNextPage
function. It works, but I'd like to improve its performance by, for example, prefetching some pages so that they don't need to be loaded with every page change. The back button is instant as the data is already loaded. You can test it here.I am using tRPC T3 stack so the query syntax is little different.
To keep track of the current page that gets rendered, I use the page number to select the appropriate page from the
data.pages
array. If the user clicks the next page button, I check the length ofdata.pages
array and if<
new page number call fetchNextPage. If the user clicks the back button, I simply use the page number to select the previously fetched page, which is already included in thedata.pages
array.Beta Was this translation helpful? Give feedback.
All reactions