Skip to content

Commit 426b505

Browse files
committed
docs: Add placeholder data guide
1 parent a126f30 commit 426b505

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

docs/src/manifests/manifest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@
115115
"path": "/guides/infinite-queries",
116116
"editUrl": "/guides/infinite-queries.md"
117117
},
118+
{
119+
"title": "Placeholder Query Data",
120+
"path": "/guides/placeholder-query-data",
121+
"editUrl": "/guides/placeholder-query-data.md"
122+
},
118123
{
119124
"title": "Initial Query Data",
120125
"path": "/guides/initial-query-data",

docs/src/pages/guides/initial-query-data.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@ id: initial-query-data
33
title: Initial Query Data
44
---
55

6-
## Initial Data
6+
There are many ways to supply initial data for a query to the cache before you need it:
77

8-
There may be times when you already have the initial data for a query synchronously available in your app. If and when this is the case, you can use the `config.initialData` option to set the initial data for a query and skip the initial loading state!
8+
- Declaratively:
9+
- Provide `initialData` to a query to prepopulate the its cache if empty
10+
- Imperatively:
11+
- [Prefetch the data using `queryClient.prefetchQuery`](../prefetching)
12+
- [Manually place the data into the cache using `queryClient.setQueryData`](../prefetching)
13+
14+
## Using `initialData` to prepopulate a query
15+
16+
There may be times when you already have the initial data for a query available in your app and can simply provide it directly to your query. If and when this is the case, you can use the `config.initialData` option to set the initial data for a query and skip the initial loading state!
17+
18+
> IMPORTANT: `initialData` is persisted to the cache, so it is not recommended to provide placeholder, partial or incomplete data to this option and instead use `placeholderData`
919
1020
```js
1121
function Todos() {
@@ -15,7 +25,7 @@ function Todos() {
1525
}
1626
```
1727

18-
## Using Initial Data with `staleTime`
28+
### Initial Data and `staleTime`
1929

2030
`initialData` is treated exactly the same as normal data, which means that is follows the same rules and expectations of `staleTime`.
2131

@@ -32,9 +42,9 @@ function Todos() {
3242

3343
> If you would rather treat your data as **prefetched data**, we recommend that you use the `prefetchQuery` or `fetchQuery` APIs to populate the cache beforehand, thus letting you configure your `staleTime` independently from your initialData
3444
35-
## Initial Data Function
45+
### Initial Data Function
3646

37-
If the process for accessing a query's initial data is intensive or just not something you want to perform on every render, you can pass a function as the `initialData` value. This function will be executed only once when the query is initialized, saving you precious memory and CPU:
47+
If the process for accessing a query's initial data is intensive or just not something you want to perform on every render, you can pass a function as the `initialData` value. This function will be executed only once when the query is initialized, saving you precious memory and/or CPU:
3848

3949
```js
4050
function Todos() {
@@ -46,7 +56,7 @@ function Todos() {
4656
}
4757
```
4858

49-
## Initial Data from Cache
59+
### Initial Data from Cache
5060

5161
In some circumstances, you may be able to provide the initial data for a query from the cached result of another. A good example of this would be searching the cached data from a todos list query for an individual todo item, then using that as the initial data for your individual todo query:
5262

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
id: placeholder-query-data
3+
title: Placeholder Query Data
4+
---
5+
6+
## What is placeholder data?
7+
8+
Placeholder data allows a query to behave as if it already has data, similar to the `initialData`, 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+
12+
There 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 the its cache if empty
16+
- Imperatively:
17+
- [Prefetch or fetch the data using `queryClient` and the `placeholderData` option](../prefetching)
18+
19+
## Placeholder Data as a Value
20+
21+
```js
22+
function Todos() {
23+
const result = useQuery('todos', () => fetch('/todos'), {
24+
placeholderData: placeholderTodos,
25+
})
26+
}
27+
```
28+
29+
### Placeholder Data as a Function
30+
31+
If the process for accessing a query's placeholder data is intensive or just not something you want to perform on every render, you can pass a function as the `placeholderData` value. This function will be executed only once when the query is placeholderized, saving you precious memory and/or CPU:
32+
33+
```js
34+
function Todos() {
35+
const result = useQuery('todos', () => fetch('/todos'), {
36+
placeholderData: () => {
37+
return generateFakeTodos()
38+
},
39+
})
40+
}
41+
```
42+
43+
### Placeholder Data from Cache
44+
45+
In 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:
46+
47+
```js
48+
function Todo({ blogPostId }) {
49+
const result = useQuery(['blogPost', blogPostId], () => fetch('/blogPosts'), {
50+
placeholderData: () => {
51+
// Use the smaller/preview version of the blogPost from the 'blogPosts' query as the placeholder data for this blogPost query
52+
return queryClient
53+
.getQueryData('blogPosts')
54+
?.find(d => d.id === blogPostId)
55+
},
56+
})
57+
}
58+
```

0 commit comments

Comments
 (0)