Creating infinite queries
Another very common UI pattern is building an infinite scroll component. In this pattern, we are presented with a list that allows us to load more data as we scroll down.
To deal with these types of lists, React Query has an alternative to the useQuery
hook, which is another custom hook called useInfiniteQuery.
Using the useInfiniteQuery
hook has many similarities to the useQuery
one, but some things differ that we need to be aware of:
- Your data is now an object that contains the following:
- The fetched pages
- The
page
parameters that were used to fetch the pages
- A function called
fetchNextPage
to fetch the next page - A function called
fetchPreviousPage
to fetch the previous page - A Boolean state called
isFetchingNextPage
to indicate that the next page is being fetched - A Boolean state called
isFetchingPreviousPage
to indicate that the next page is being fetched - A Boolean state called
hasNextPage
to indicate whether the list has...