I'm trying to implement useInfiniteQuery for pagination scroll with filters for a backend that doesn't support pagination cursor (I'm using limit and start parameters). I was able to retrieve my data but my hasNextPage variable is always undefined. I cannot fetch more data. When the button is used to trigger fetchNextPage, new data can be seen but it removes the previous data.
What am I doing wrong ? Here is my code:
// Query data
const {
data,
status,
error,
isSuccess,
hasNextPage,
fetchNextPage,
isFetching,
isFetchingNextPage,
isLoading,
isError,
} = useInfiniteQuery(
[
'vendors',
{vendor_type: vendorTypesId},
{country: countryId},
{city: cityId},
{vendor_product_types: productCategoryId},
{page: thisPage},
{start: start},
{name: nameInput},
{limit: limit},
],
getVendors,
{
initialData: {
pages : [{
result: vendors,
resultCount: 495,
pageId: 0,
nextPageId: 15
}],
pageParams: startParams
}
},
{getNextPageParam: (lastPage, pages) => lastPage.nextPageId}
{keepPreviousData: true}
)
Update: hasNextPage was always undefined beacause getNextPageParam and initialData were in two separated objects. I corrected it like this :
{
initialData: {
pages : [{
result: vendors,
resultCount: 495,
pageId: 0,
nextPageId: 15
}],
pageParams: startParams
},
getNextPageParam: (lastPage, pages) => lastPage.nextPageId
}
It now returns true but clicking on the button still erase the previous data.