T
TanStack15mo ago
sensitive-blue

Pagination with infinite query...

Hey everyone... i'm thinking to use infinite query to retrieve data for a table with pagination... but probably is not the right choice because if i go back my pages result grows up... what am I doing wrong?.... for example if i look this:
useInfiniteQuery({
queryKey: ['projects'],
queryFn: fetchProjects,
initialPageParam: 0,
getNextPageParam: (lastPage, pages) => lastPage.nextCursor,
getPreviousPageParam: (firstPage, pages) => firstPage.prevCursor,
})
useInfiniteQuery({
queryKey: ['projects'],
queryFn: fetchProjects,
initialPageParam: 0,
getNextPageParam: (lastPage, pages) => lastPage.nextCursor,
getPreviousPageParam: (firstPage, pages) => firstPage.prevCursor,
})
and i have 2 page... i will have this output... step1: pages Array(1) go forward: Array(2) go back: Array(3) with the same result of step1
6 Replies
xenophobic-harlequin
xenophobic-harlequin15mo ago
is go back calling fetchPreviousPage()?
sensitive-blue
sensitive-blueOP15mo ago
yes if i go forward it work fine... but i go back the previus page is add to the pages array maybe it has to work like this... that is, if I go back does it call the previous cursor or add it? I expected it to get the cached one
xenophobic-harlequin
xenophobic-harlequin15mo ago
fetchNext / fetchPrevious always adds, never reads from the cache
sensitive-blue
sensitive-blueOP15mo ago
ahhh ok... so if i want use react-query to create a table with pagination probably i have to use useQuery and not useInfiniteQuery... is it right?
xenophobic-harlequin
xenophobic-harlequin15mo ago
yes, paginated queries where only one page at a time is displayed is not a use-case for infinite queries
sensitive-blue
sensitive-blueOP15mo ago
Thank you

Did you find this page helpful?