Blog>
Snippets

Integrating Pagination and Infinite Scroll with Filters

Illustrate the integration of pagination or infinite scrolling features with filters in a React Query setup, showing how to maintain the user's position and loaded data state across filter changes.
import { useInfiniteQuery } from 'react-query';

const fetchProjects = async (page = 0, filters) => {
  const filterString = filters ? `?${Object.keys(filters).map(key => `${key}=${filters[key]}`).join('&')}` : '';
  const res = await fetch(`/api/projects?page=${page}${filterString}`);
  return res.json();
};
Defines a function to fetch projects from an API. This function takes in a page number and filters, constructing a query string for the filters to attach to the API request URL. It returns the JSON response.
const useFetchProjects = (filters) => {
  return useInfiniteQuery(
    ['projects', filters],
    ({ pageParam = 0 }) => fetchProjects(pageParam, filters),
    {
      getNextPageParam: (lastPage, pages) => lastPage.nextPage ?? false
    }
  );
};
Utilizes the useInfiniteQuery hook from React Query to fetch projects in an infinite scrolling manner. The query key is an array with 'projects' and the current filters, ensuring that the query is refetched when filters change. getNextPageParam returns the next page number if available, indicating there are more pages to load.
const MyComponent = () => {
  const [filters, setFilters] = useState({});
  const { data, hasNextPage, fetchNextPage } = useFetchProjects(filters);

  const handleFilterChange = (newFilters) => {
    setFilters(newFilters);
  };

  return (
    <div>
      {/* Filter inputs and onChange event handlers calling handleFilterChange */}
      {data.pages.map((page, i) => (
        <div key={i}>{/* Render projects */}</div>
      ))}
      {hasNextPage && <button onClick={() => fetchNextPage()}>Load More</button>}
    </div>
  );
};
Implements a component to display projects and handle infinite scrolling with filters. It uses the useFetchProjects hook and renders a 'Load More' button if more pages are available. Filter changes are handled by updating the local state, which triggers a refetch of the projects with the new filters.