Open History to see the last commit
1---
2id: placeholder-query-data
3title: Placeholder Query Data
4---
5β
6## What is placeholder data?
7β
8Placeholder data allows a query to behave as if it already has data, similar to the `initialData` option, but **the data is not persisted to the cache**. This comes in handy for situations where you have enough partial (or fake) data to render the query successfully while the actual data is fetched in the background.
9β
10> Example: An individual blog post query could pull "preview" data from a parent list of blog posts that only include title and a small snippet of the post body. You would not want to persist this partial data to the query result of the individual query, but it is useful for showing the content layout as quickly as possible while the actual query finishes to fetch the entire object.
11β
12There are a few ways to supply placeholder data for a query to the cache before you need it:
13β
14- Declaratively:
15 - Provide `placeholderData` to a query to prepopulate its cache if empty
16- Imperatively:
17 - [Prefetch or fetch the data using `queryClient` and the `placeholderData` option](../guides/prefetching)
18β
19## Placeholder Data as a Value
20β
21[//]: # 'Example'
22β
23```tsx
24function Todos() {
25 const result = useQuery({
26 queryKey: ['todos'],
27 queryFn: () => fetch('/todos'),
28 placeholderData: placeholderTodos,
29 })
30}
31```
32β
33[//]: # 'Example'
34[//]: # 'Memoization'
35β
36### Placeholder Data Memoization
37β
38If the process for accessing a query's placeholder data is intensive or just not something you want to perform on every render, you can memoize the value:
39β
40[//]: # 'Memoization'
41[//]: # 'Example2'
42β
43```tsx
44function Todos() {
45 const placeholderData = useMemo(() => generateFakeTodos(), [])
46 const result = useQuery({
47 queryKey: ['todos'],
48 queryFn: () => fetch('/todos'),
49 placeholderData,
50 })
51}
52```
53β
54[//]: # 'Example2'
55β
56### Placeholder Data from Cache
57β
58In some circumstances, you may be able to provide the placeholder data for a query from the cached result of another. A good example of this would be searching the cached data from a blog post list query for a preview version of the post, then using that as the placeholder data for your individual post query:
59β
60[//]: # 'Example3'
61β
62```tsx
63function Todo({ blogPostId }) {
64 const result = useQuery({
65 queryKey: ['blogPost', blogPostId],
66 queryFn: () => fetch(`/blogPosts/${blogPostId}`),
67 placeholderData: () => {
68 // Use the smaller/preview version of the blogPost from the 'blogPosts'
69 // query as the placeholder data for this blogPost query
70 return queryClient
71 .getQueryData(['blogPosts'])
72 ?.find((d) => d.id === blogPostId)
73 },
74 })
75}
76```
77β
78[//]: # 'Example3'
79[//]: # 'Materials'
80β
81## Further reading
82β
83For a comparison between `Placeholder Data` and `Initial Data`, have a look at the [Community Resources](../community/tkdodos-blog#9-placeholder-and-initial-data-in-react-query).
84β
85[//]: # 'Materials'