Basic Pagination
Implement pagination usinguseQuery with a page parameter in the query key:
Each page is cached separately based on its unique query key
['projects', page]. This means navigating back to a previously viewed page will be instant if the data is still in cache.Keeping Previous Data
UseplaceholderData with keepPreviousData to prevent UI flickering when transitioning between pages:
Prefetching Next Page
Improve perceived performance by prefetching the next page before the user clicks:1
Check placeholder status
Only prefetch when not showing placeholder data to avoid prefetching the wrong page.
2
Check if more pages exist
Use your API’s
hasMore or similar flag to determine if there’s a next page.3
Prefetch the next page
Use
queryClient.prefetchQuery() to fetch and cache the next page in the background.Cursor-Based Pagination
For APIs using cursor-based pagination instead of page numbers:Offset-Based Pagination
For APIs using offset and limit:Page Navigation UI
Implement traditional page number navigation:Stale Time Configuration
Control how long paginated data stays fresh:Setting a
staleTime prevents unnecessary refetches when navigating between pages quickly. Each page’s staleness is tracked independently.