Skip to content

Commit e76a2c3

Browse files
committed
docs: new docs for placeholderData
1 parent 2fc597a commit e76a2c3

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

docs/react/guides/placeholder-query-data.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ There are a few ways to supply placeholder data for a query to the cache before
1616
- Imperatively:
1717
- [Prefetch or fetch the data using `queryClient` and the `placeholderData` option](../guides/prefetching)
1818

19+
When we use `placeholderData`, our Query will not be in a `pending` state - it will start out as being in `success` state, because we have `data` to display - even if that data is just "placeholder" data. To distinguish it from "real" data, we will also have the `isPlaceholderData` flag set to `true` on the Query result.
20+
1921
## Placeholder Data as a Value
2022

21-
[//]: # 'Example'
23+
[//]: # 'ExampleValue'
2224

2325
```tsx
2426
function Todos() {
@@ -30,16 +32,13 @@ function Todos() {
3032
}
3133
```
3234

33-
[//]: # 'Example'
35+
[//]: # 'ExampleValue'
3436
[//]: # 'Memoization'
3537

3638
### Placeholder Data Memoization
3739

3840
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 memoize the value:
3941

40-
[//]: # 'Memoization'
41-
[//]: # 'Example2'
42-
4342
```tsx
4443
function Todos() {
4544
const placeholderData = useMemo(() => generateFakeTodos(), [])
@@ -51,13 +50,25 @@ function Todos() {
5150
}
5251
```
5352

54-
[//]: # 'Example2'
53+
[//]: # 'Memoization'
54+
55+
## Placeholder Data as a Function
56+
57+
`placeholderData` can also be a function, where you can get access to the data and Query meta information of a "previous" successful Query. This is useful for situations where you want to use the data from one query as the placeholder data for another query. When the QueryKey changes, e.g. from `['todos', 1]` to `['todos', 2]`, we can keep displaying "old" data instead of having to show a loading spinner while data is _transitioning_ from one Query to the next. For more information, see [Paginated Queries](../guides/paginated-queries).
58+
59+
```tsx
60+
const result = useQuery({
61+
queryKey: ['todos', id],
62+
queryFn: () => fetch(`/todos/${id}`),
63+
placeholderData: (previousData, previousQuery) => previousData,
64+
})
65+
```
5566

5667
### Placeholder Data from Cache
5768

5869
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:
5970

60-
[//]: # 'Example3'
71+
[//]: # 'ExampleCache'
6172

6273
```tsx
6374
function Todo({ blogPostId }) {
@@ -75,7 +86,7 @@ function Todo({ blogPostId }) {
7586
}
7687
```
7788

78-
[//]: # 'Example3'
89+
[//]: # 'ExampleCache'
7990
[//]: # 'Materials'
8091

8192
## Further reading

docs/react/reference/useQuery.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ const {
148148
- `initialDataUpdatedAt: number | (() => number | undefined)`
149149
- Optional
150150
- If set, this value will be used as the time (in milliseconds) of when the `initialData` itself was last updated.
151-
- `placeholderData: TData | (previousValue: TData) => TData`
151+
- `placeholderData: TData | (previousValue: TData | undefined; previousQuery: Query | undefined,) => TData`
152152
- Optional
153-
- If set, this value will be used as the placeholder data for this particular query observer while the query is still in the `pending` data and no initialData has been provided.
153+
- If set, this value will be used as the placeholder data for this particular query observer while the query is still in the `pending` state.
154154
- `placeholderData` is **not persisted** to the cache
155-
- If you provide a function for `placeholderData`, as a first argument you will receive previously watched query data if available
155+
- If you provide a function for `placeholderData`, as a first argument you will receive previously watched query data if available, and the second argument will be the complete previousQuery instance.
156156
- `structuralSharing: boolean | ((oldData: TData | undefined, newData: TData) => TData)`
157157
- Optional
158158
- Defaults to `true`

docs/vue/guides/placeholder-query-data.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Placeholder Query Data
44
ref: docs/react/guides/placeholder-query-data.md
55
---
66

7-
[//]: # 'Example'
7+
[//]: # 'ExampleValue'
88

99
```tsx
1010
const result = useQuery({
@@ -14,12 +14,10 @@ const result = useQuery({
1414
})
1515
```
1616

17-
[//]: # 'Example'
17+
[//]: # 'ExampleValue'
1818
[//]: # 'Memoization'
1919
[//]: # 'Memoization'
20-
[//]: # 'Example2'
21-
[//]: # 'Example2'
22-
[//]: # 'Example3'
20+
[//]: # 'ExampleCache'
2321

2422
```tsx
2523
const result = useQuery({
@@ -35,6 +33,6 @@ const result = useQuery({
3533
})
3634
```
3735

38-
[//]: # 'Example3'
36+
[//]: # 'ExampleCache'
3937
[//]: # 'Materials'
4038
[//]: # 'Materials'

0 commit comments

Comments
 (0)