T
TanStack3y ago
harsh-harlequin

What is the correct way of using useInfiniteQuery?

const [pagination, setPagination] = useState({
offset: parseInt(router.query.offset as string) || 0,
limit: parseInt(router.query.limit as string) || 10,
});
const [pagination, setPagination] = useState({
offset: parseInt(router.query.offset as string) || 0,
limit: parseInt(router.query.limit as string) || 10,
});
const {
data,
isFetching,
isLoading,
fetchPreviousPage,
isFetchingPreviousPage,
fetchNextPage,
isFetchingNextPage,
} = useInfiniteQuery({
queryKey: ['tableData', pagination],
queryFn: async ({ pageParam = pagination }) => {
const response: ApiResponse<T> = await fetch(
resourceUrl + `?offset=${pageParam.offset}&limit=${pageParam.limit}`,
).then((res) => res.json());

return response;
},
keepPreviousData: true,
refetchOnWindowFocus: false,
});
const {
data,
isFetching,
isLoading,
fetchPreviousPage,
isFetchingPreviousPage,
fetchNextPage,
isFetchingNextPage,
} = useInfiniteQuery({
queryKey: ['tableData', pagination],
queryFn: async ({ pageParam = pagination }) => {
const response: ApiResponse<T> = await fetch(
resourceUrl + `?offset=${pageParam.offset}&limit=${pageParam.limit}`,
).then((res) => res.json());

return response;
},
keepPreviousData: true,
refetchOnWindowFocus: false,
});
fetchPreviousPage({
pageParam: {
offset: offset - 10,
limit: pageSize,
},
});
fetchPreviousPage({
pageParam: {
offset: offset - 10,
limit: pageSize,
},
});
3 Replies
harsh-harlequin
harsh-harlequinOP3y ago
IS this the correct way? feels a bit off...
harsh-harlequin
harsh-harlequinOP3y ago
No description
harsh-harlequin
harsh-harlequinOP3y ago
its not utilizing the previously fetched pages / pageParams

Did you find this page helpful?