T
TanStack14mo ago
ambitious-aqua

Tanstack useInfiniteQuery hooks missing last page and hasNextPage return false

hello there I am using useInfiniteQuery for infinite scrolling, from backend I am getting data in this formate when I hit this querry.
// function to get data

const getAllTransactions = async ({ pageParam }: any) => {
try {
const { data } = await axios.get(`${BASE_URL}transactions?page=${pageParam}&type=${type}`, { headers: { Authorization: `Bearer ${session?.access_token}` } })
return data
} catch (error) {
return error

}
}


// using the getAllTransactions function with useInfiniteQuery hook
const { data, hasNextPage, fetchNextPage, isFetchingNextPage, status }: any = useInfiniteQuery({
queryKey: [type, 'getAllTransactions'],
queryFn: getAllTransactions,
initialPageParam: 1,
getNextPageParam: (lastPage) => lastPage.next_page ? lastPage.next_page : undefined,
})


const response = {
data:[.......],
"total_page": 8,
"next_page": 2,
"previous_page": null
}
// function to get data

const getAllTransactions = async ({ pageParam }: any) => {
try {
const { data } = await axios.get(`${BASE_URL}transactions?page=${pageParam}&type=${type}`, { headers: { Authorization: `Bearer ${session?.access_token}` } })
return data
} catch (error) {
return error

}
}


// using the getAllTransactions function with useInfiniteQuery hook
const { data, hasNextPage, fetchNextPage, isFetchingNextPage, status }: any = useInfiniteQuery({
queryKey: [type, 'getAllTransactions'],
queryFn: getAllTransactions,
initialPageParam: 1,
getNextPageParam: (lastPage) => lastPage.next_page ? lastPage.next_page : undefined,
})


const response = {
data:[.......],
"total_page": 8,
"next_page": 2,
"previous_page": null
}
now I am rendering a button if hasNextPage returns true, but it return true before one page earlier. what I am doing wrong here,
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?