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.
More Dev Tips
Discover more tips and tricks to level up your development skills
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 startedStay up to date
Sign up for our newsletter and we will keep you updated on everything going on with supastarter.