Skip to content

Instantly share code, notes, and snippets.

@andrioid
Created May 8, 2024 13:01
Show Gist options
  • Save andrioid/ed103d6a55c494c76d2a9a7a66e98364 to your computer and use it in GitHub Desktop.
Save andrioid/ed103d6a55c494c76d2a9a7a66e98364 to your computer and use it in GitHub Desktop.
Fetch all the pages with useInfiniteQuery
const uq = useInfiniteQuery({
queryKey: ["all_contacts"],
queryFn: ({ pageParam }) =>
api.getContacts({
page: pageParam,
}),
getNextPageParam: (lastPage) => {
if (lastPage.pagination.hasNextPage === false) return null;
return lastPage.pagination.currentPage + 1;
},
initialPageParam: 1,
});
useEffect(() => {
if (!uq.isSuccess) return;
if (uq.hasNextPage) {
uq.fetchNextPage();
}
}, [uq.isSuccess]);
const items = useMemo(() => {
return uq.data?.pages.flatMap((page) => page.data) || [];
}, [uq.data]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment