initialData, placeholder data is not persisted to the cache and doesn’t affect the query’s state.
Basic Usage
Provide placeholder data using theplaceholderData option:
The
isPlaceholderData flag tells you when you’re showing placeholder data vs. real data.Keep Previous Data
A common pattern is keeping the previous query’s data while fetching new data. Use thekeepPreviousData helper:
Placeholder Data Function
Compute placeholder data dynamically using a function:With Select Transform
Placeholder data works correctly withselect transforms:
1
Previous data is kept
The previous query result is used as placeholder data
2
Select function runs
The
select transform runs on the placeholder data3
New data replaces placeholder
When the fetch completes, real data replaces the placeholder
Query State Behavior
Placeholder data affects the query state differently than initial data:Pagination Example
A complete pagination example usingkeepPreviousData:
TypeScript
Placeholder data must match the query’s data type:When to Use Placeholder Data
keepPreviousData Implementation
ThekeepPreviousData helper is a simple function that returns the previous data:
Differences from Initial Data
See Also
- Initial Data - For persisting data to the cache
- Paginated Queries - Complete pagination guide
- Optimistic Updates - For mutation optimistic UI