Use keepPreviousData for smooth paginated queries

8/24/2025

#tanstack#react#ux

When building pagination with TanStack Query, you might notice the UI briefly "flashes empty" while loading the next page.
That’s because the old data is cleared as soon as the query key changes.

You can fix this with keepPreviousData.
It tells Tanstack Query to keep showing the old result until the new one arrives.

import { keepPreviousData, useQuery } from '@tanstack/react-query'
 
const { data, isLoading, isFetching } = useQuery({
  queryKey: ["projects", page],
  queryFn: () => fetchProjects(page),
  placeholderData: keepPreviousData,
});

Now, when page changes:

  • The old page data stays visible
  • A loading spinner can still indicate fetching
  • The transition feels much smoother

Perfect for paginated lists, infinite scroll, or any UI where flickering content hurts UX.

New: Your Idea. Our Experts. We'll Build Your SaaS For You.

Our 'Done For You' service simplifies the process, taking your concept and delivering a production-ready MVP or complete SaaS solution.

Start your scalable and production-ready SaaS today

Save endless hours of development time and focus on what's important for your customers with our SaaS starter kits for Next.js, Nuxt 3 and SvelteKit

Get started

Stay up to date

Sign up for our newsletter and we will keep you updated on everything going on with supastarter.