Created
May 8, 2024 13:01
-
-
Save andrioid/ed103d6a55c494c76d2a9a7a66e98364 to your computer and use it in GitHub Desktop.
Fetch all the pages with useInfiniteQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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