How do I implement getNextPageParam to do Infinite/paginated queries without a cursor? #5320
-
The API I need to query only has the following parameters I can send in a post request to do pagination:
I already managed to configure the query function to send pageParam as a post parameter for offset but unfortunately I don't have any info about which page we are on or which page is the next one on the API results themselves, meaning I have no idea what to return as the next page in getNextPageParam. Is there any way to make getNextPageParam work at all with this configuration? Can I keep an internal index of the current page somehow? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hi 👋 The first argument to getNextPageParam is the last page. Typically, APIs that use a limit/offset pagination approach provide the current limit and offset as metadata in the response. Can you use this to increment the offset based on the last page in getNextPageParam? |
Beta Was this translation helpful? Give feedback.
-
in v5, https://tanstack.com/query/v5/docs/react/reference/useInfiniteQuery |
Beta Was this translation helpful? Give feedback.
in v5,
getNextPageParam
andgetPreviousPageParam
will get access to all "pageParams", similar to how it gets access to all pages. So, you can derive the info from that. Until then, what @louis-young said is the best option: return that info with your data from the QueryFunction.https://tanstack.com/query/v5/docs/react/reference/useInfiniteQuery