Skip to content
Source
Sign inSign up
TanStack/query

pass `pageParams` of previous pages to `getNextPageParam` and `getPreviousPageParam` #5089

Send to my agent
ClosedTkDodo opened this issue 9 comments
TkDodocommented

No description provided.

  1. TkDodo added labelsenhancement

  2. TkDodo added labelsv5

  3. @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:

    getPreviousPageParam: (firstPage, allPages, firstPageParam, allPageParams, currentPageIndex) => {
      return currentPageIndex === 0 ? firstPageParam : allPageParams[currentPageIndex-1]
    }
  4. 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

  5. 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 and maxPages is 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".

  6. 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 defaultPageParam of 10:

    {
      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. getNextPageParam is easy:

    getNextPageParam(lastPage) => lastPage.nextCursor

    I'm not sure how we would implement getPreviousPageParam here, no matter what values we provide. Can you show an example implementation with your approach?

  7. From your example. Let's say with fetch 2 next pages with maxPages = 3 Let's keep all the pageParams history in a new virtualPageParams array (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) : undefined

    previousPageParam equals 13 in this example.

    So when maxPages is set and getPreviousNextPage is not set, we can use this solution to get the previous page param (except for the first initial page).

  8. I see, so you're suggesting we should store all the pageParams of everything that we've ever fetched.. I think this would be a rather big change, because right now, we throw that away with maxPages being set.

  9. TkDodo closed this

  10. if you think this feature makes sense, I can check the impacts and if there are only minor changes, push a PR.

  11. I think I'd rather wait to see if this is a problem for someone

  12. 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 pageParam together 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.