乐闻世界logo
搜索文章和话题

How to use useInfiniteQuery with React-query ?

1个答案

1

The useInfiniteQuery hook in React Query is used to implement an infinite scroll data loading pattern. This hook allows you to load data incrementally by page number or any other logic, and dynamically loads more data into the list as the user scrolls or interacts.

When using useInfiniteQuery, you need to provide a unique cache key and a function to fetch data. This function receives an object containing parameters required to fetch the next page, such as pageParam.

Here is a basic example of how to use useInfiniteQuery:

jsx
import { useInfiniteQuery } from 'react-query'; import axios from 'axios'; // Assuming we have an API that returns specific pages based on the page number const fetchProjects = ({ pageParam = 1 }) => { return axios.get('/api/projects?page=' + pageParam); }; function Projects() { // Using the `useInfiniteQuery` hook const { data, error, fetchNextPage, hasNextPage, isFetchingNextPage, status, } = useInfiniteQuery('projects', fetchProjects, { // Parameters for fetching the next page getNextPageParam: (lastPage, pages) => { // Determine the next page's parameters based on the last page's data and existing pages array return lastPage.nextPage ?? false; // Assuming the API response includes next page parameters }, }); // Render the component return ( <div> {status === 'loading' && <p>Loading...</p>} {status === 'error' && <p>Error: {error.message}</p>} {data?.pages.map((group, i) => ( <React.Fragment key={i}> {group.items.map(project => ( <p key={project.id}>{project.name}</p> ))} </React.Fragment> ))} <div> <button onClick={() => fetchNextPage()} disabled={!hasNextPage || isFetchingNextPage} > {isFetchingNextPage ? 'Loading more...' : hasNextPage ? 'Load More' : 'Nothing more to load'} </button> </div> </div> ); } export default Projects;

In this example, fetchProjects is a function that calls the API to fetch project data. We use useInfiniteQuery to load this data, with the first parameter being the cache key ('projects'), the second parameter being the data-fetching function, and the third parameter being a configuration object containing the getNextPageParam function. This function determines how to fetch the next page of data.

useInfiniteQuery returns several properties and methods:

  • data: an object containing the data loaded per page.
  • error: contains error information.
  • fetchNextPage: a function to load the next page of data.
  • hasNextPage: a boolean indicating whether more pages can be loaded.
  • isFetchingNextPage: a boolean indicating whether the next page is currently being loaded.
  • status: the request status, which can be 'loading', 'error', or 'success'.

In the UI, we render the data loaded per page and provide a button at the bottom to load more data. The button's disabled state depends on whether there is a next page and whether the next page is currently being loaded.

This is a simplified example; in actual applications, you may need to consider additional factors such as cache management, data synchronization, and error handling.

2024年6月29日 12:07 回复

你的答案