Progressive rendering
A list has enough summary fields to render rows before detail requests finish.
TanStack · Pipe
placeholderData lets a query render with temporary data while the real request runs. Unlike initialData, the temporary value is not persisted in the cache and is marked with isPlaceholderData.
placeholderData supplies observer-level temporary data so a query can render successfully while its request runs. The temporary value is not written to the cache; the result exposes isPlaceholderData so the UI can distinguish it from fetched data.
useQuery({ queryKey, queryFn, placeholderData })| Name | Type | Description |
|---|---|---|
placeholderData | TData | ((previousData: TData | undefined, previousQuery: Query) => TData) | A temporary value or function producing temporary query data; it is used while the real data is fetched and is not persisted in the cache. |
A query result containing the temporary data, with isPlaceholderData set to true while that data is displayed; the fetched result replaces it when available.
import { useQuery } from '@tanstack/react-query'
export function TodoTitle({ id }: { id: number }) {
const query = useQuery({
queryKey: ['todo', id],
queryFn: () => fetch(`/api/todos/${id}`).then((r) => r.json()),
placeholderData: { id, title: 'Loading…' },
})
return <h2>{query.data.title}</h2>
}The component renders immediately with temporary todo data while the real response is fetched in the background.
A list has enough summary fields to render rows before detail requests finish.
A page-number query changes keys and should keep the previous page visible during refetching.
A skeleton or lightweight fake object can provide the shape required by the view immediately.
placeholderData makes a query behave as though it already has data while the actual request proceeds. Unlike initialData, the supplied value is not persisted to the cache.
Follow-up: How can the UI identify the temporary state? Check the result's isPlaceholderData flag, which is true while placeholder data is being supplied.
A query using placeholderData starts in the success state rather than pending because it has displayable data. The real fetch still runs in the background and replaces the temporary result when it completes.
Follow-up: Why is this useful for loading states? It allows partial or fake data to render without showing an empty pending view.
placeholderData may be a function. That function can receive data and query metadata from a previous successful query, enabling the prior result to remain visible during a key change.
Follow-up: What common pagination pattern follows from this? Return previousData from the placeholder function so old results remain visible until the new page arrives.
initialDataisPlaceholderDatakeepPreviousDataqueryClientuseQuery