pass `pageParams` of previous pages to `getNextPageParam` and `getPreviousPageParam` #5089
Send to my agentNo description provided.
TkDodo added labelsenhancement
TkDodo added labelsv5
Hmm not sure I get why we need that. getPreviousPageParam is always called with the first page. The feature is exactly made to make it work with APIs that don't have a previous cursor: https://tanstack.com/query/v5/docs/react/guides/infinite-queries#what-if-my-api-doesnt-return-a-cursor
Sure, this feature is great for a page number based pagination (you can do a
firstPageParam - 1) Let me clarify my initial comment: I was thinking about a solution to get a previous page param when the API is cursor based without prevCursor feature andmaxPagesis set. For the first page we cannot guess the previous cursor value. But when fetching more pages we could keep the history of all the page params and the absolute page index. But this can be confusing to have an actual pageParams and a "virtual one".But when fetching more pages we could keep the history of all the page params and the absolute page index.
I still don't understand, maybe an example would help. Let's assume we have the following structure in our cache for a
defaultPageParamof10:{ pages: [ { data: ["10,11,12"], nextCursor: 13 }, { data: ["13,14,15"], nextCursor: 16 }, { data: ["16,17,18"], nextCursor: 19 }, ], pageParams: [0,3,6] }The api is cursor based, so we just get a
nextCursor.getNextPageParamis easy:getNextPageParam(lastPage) => lastPage.nextCursorI'm not sure how we would implement
getPreviousPageParamhere, no matter what values we provide. Can you show an example implementation with your approach?From your example. Let's say with fetch 2 next pages with
maxPages= 3 Let's keep all thepageParamshistory in a newvirtualPageParamsarray (better naming to find) And also keep the current first page absolute index.{ pages: [ { data: ["16,17,18"], nextCursor: 19 }, { data: ["19,20,21"], nextCursor: 22 }, { data: ["22,23,24"], nextCursor: 25 }, ], pageParams: [16,19,22], virtualPageParams: [10,13,16,19,22], // only needed when maxPages is defined firstPageIndex: 2, // only needed when maxPages is defined }So now we can compute the
previousPageParam:previousPageParam = firstPageIndex > 0 ? virtualPageParams(firstPageIndex-1) : undefinedpreviousPageParamequals 13 in this example.So when
maxPagesis set andgetPreviousNextPageis not set, we can use this solution to get the previous page param (except for the first initial page).I see, so you're suggesting we should store all the
pageParamsof everything that we've ever fetched.. I think this would be a rather big change, because right now, we throw that away withmaxPagesbeing set.TkDodo closed this
if you think this feature makes sense, I can check the impacts and if there are only minor changes, push a PR.
I think I'd rather wait to see if this is a problem for someone
This is exactly the issue I am having. The API doesn't return anything related to paging. So we have to use the pageParam of the previous page.
It is somewhat easy to work-around by storing the
pageParamtogether with the page data. But it would be nice to have a built-in way to do this.Edit: I just noticed, I guess this is available in v5. Looking forward to it.
Sign in to join this conversation.
@TkDodo This new feature is great :smiley: but should we also pass the current page number/index? It could be handy if the API doesn't return the previous cursor and you are on page number 3 and you want to get the previous cursor to fetch the previous page, you can do: