Capítulo 25 de 80
placeholderData makes a query render as if it already has data (starting in success state, not pending) without persisting anything to the cache — the mechanism for showing partial/preview/fake data while the real fetch happens in the background.
initialData: this is the core distinction from Chapter 24 — placeholder data is display-only, never written into the actual cache entry.placeholderData starts in status: 'success'/isSuccess: true with isPlaceholderData: true, letting you distinguish "this is fill-in data" from "this is the real fetched result" without a separate loading branch.placeholderData.useMemo so it isn't recomputed every render.placeholderData: (previousData, previousQuery) => ... gives access to the previous successful query's data/meta — this is exactly the mechanism keepPreviousData (Chapter 22) is built from, for carrying old data across a changing key.// Static placeholder value
useQuery({ queryKey: ['todos'], queryFn: () => fetch('/todos'), placeholderData: placeholderTodos })
// Sourced from a sibling query's cache — instant "preview" render
function BlogPost({ blogPostId }) {
const queryClient = useQueryClient()
return useQuery({
queryKey: ['blogPost', blogPostId],
queryFn: () => fetch(`/blogPosts/${blogPostId}`),
placeholderData: () =>
queryClient.getQueryData(['blogPosts'])?.find((d) => d.id === blogPostId),
})
}
placeholderData (never initialData) for genuinely partial/fake/preview data — persisting incomplete data via initialData risks that shape leaking into real cache reads elsewhere.isPlaceholderData whenever the UI needs to tell placeholder content apart from the real fetched result (e.g. before enabling an action that depends on complete data).keepPreviousData, built on the function form of placeholderData shown here.