T
TanStack13mo ago
flat-fuchsia

I have some question regarding pagination

Hello we are doing a fullstack project and we have a fetch query
const LIMIT = 7

export const getHoldings = async ({pageParam}: {pageParam: number}) => {
return (
await AxiosInstance.get(`/asset/user?page=${pageParam}&limit=${LIMIT}`)
).data
}

// this on the hook
export const useEmployeesWithHoldings = () => {
return useInfiniteQuery({
initialPageParam: 1,
queryKey: ['usersWithHoldings'],
queryFn: ({ pageParam = 1 }) => getHoldings({ pageParam }),
getNextPageParam: (lastPage, allPages) => {
return lastPage.totalPages > allPages.length ? allPages.length + 1 : undefined;
},
});

}
const LIMIT = 7

export const getHoldings = async ({pageParam}: {pageParam: number}) => {
return (
await AxiosInstance.get(`/asset/user?page=${pageParam}&limit=${LIMIT}`)
).data
}

// this on the hook
export const useEmployeesWithHoldings = () => {
return useInfiniteQuery({
initialPageParam: 1,
queryKey: ['usersWithHoldings'],
queryFn: ({ pageParam = 1 }) => getHoldings({ pageParam }),
getNextPageParam: (lastPage, allPages) => {
return lastPage.totalPages > allPages.length ? allPages.length + 1 : undefined;
},
});

}
Now the data I get from the backend is this { data: [] totalPages: number, all: number (all documents) } So how would I fetch and map how to get the total pages to limit it
4 Replies
adverse-sapphire
adverse-sapphire13mo ago
So how would I fetch and map how to get the total pages to limit it
I don't understand that question. can you rephrase please?
flat-fuchsia
flat-fuchsiaOP13mo ago
My main question is how can I set max pages when I dont know how many are there to begin I get it from the request to the backend
flat-fuchsia
flat-fuchsia13mo ago
If the current data is less than limit, then you are on the last page.
adverse-sapphire
adverse-sapphire13mo ago
If you're talking about the maxPages option, that is something that you can set to limit how much is stored in the cache. For this setting, it doesn't matter how many pages there actually are. If you want to always store everything, you don't need to set maxPages

Did you find this page helpful?