T
TanStack•3y ago
rival-black

useInfiniteQuery pageParams always undefined

I am trying to get a useInfiniteQuery hook working by setting the pageParams to a string that i pass to our endpoint, but pageParams is always undefined. Does pageParams need to be a number? We use a string of the next id to fetch but it doesnt set it..
export const useInfiniteCustomers = ({
companyId,
ucId,
role,
cancel = false,
}: {
companyId: string
ucId: string
role: string
exclusiveStartKey?: string
cancel?: boolean
}) => {
return useInfiniteQuery({
queryKey: ['getInfiniteCustomers', { companyId, ucId, role }],
queryFn: async ({ pageParam }) => {
const { data } = await CustomersAPI.getInfiniteCustomers({
companyId,
ucId,
role,
...(pageParam && pageParam !== undefined && { exclusiveStartKey: pageParam }),
cancel,
})

return data.listCustomerReferencesByRole
},
// enabled: !!companyId && !!ucId && !!role,
getNextPageParam: (lastPage: any, allPages) => {
console.log('lastPage', lastPage)
return lastPage?.nextExclusiveStartKey ?? undefined
},
staleTime: Infinity,
})
}
export const useInfiniteCustomers = ({
companyId,
ucId,
role,
cancel = false,
}: {
companyId: string
ucId: string
role: string
exclusiveStartKey?: string
cancel?: boolean
}) => {
return useInfiniteQuery({
queryKey: ['getInfiniteCustomers', { companyId, ucId, role }],
queryFn: async ({ pageParam }) => {
const { data } = await CustomersAPI.getInfiniteCustomers({
companyId,
ucId,
role,
...(pageParam && pageParam !== undefined && { exclusiveStartKey: pageParam }),
cancel,
})

return data.listCustomerReferencesByRole
},
// enabled: !!companyId && !!ucId && !!role,
getNextPageParam: (lastPage: any, allPages) => {
console.log('lastPage', lastPage)
return lastPage?.nextExclusiveStartKey ?? undefined
},
staleTime: Infinity,
})
}
6 Replies
fair-rose
fair-rose•3y ago
What is the value of lastPage.nextExclusiveStartKey in the console.log?
rival-black
rival-blackOP•3y ago
its a stringify'd object that looks like:
"{"PK":"CUS#some_id","SK":"REF#some_reference"}"
"{"PK":"CUS#some_id","SK":"REF#some_reference"}"
fair-rose
fair-rose•3y ago
Odd, it should work as far as I know
conscious-sapphire
conscious-sapphire•3y ago
pageParam is what you return from getNextPageParam. It can be anything
rival-black
rival-blackOP•3y ago
@TkDodo 🔮 is there a reason why this would return undefined every time then given the code above?
conscious-sapphire
conscious-sapphire•3y ago
Not that i can see. Show a codesandbox please

Did you find this page helpful?