You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/react/guides/placeholder-query-data.md
+19-8Lines changed: 19 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -16,9 +16,11 @@ There are a few ways to supply placeholder data for a query to the cache before
16
16
- Imperatively:
17
17
-[Prefetch or fetch the data using `queryClient` and the `placeholderData` option](../guides/prefetching)
18
18
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
+
19
21
## Placeholder Data as a Value
20
22
21
-
[//]: #'Example'
23
+
[//]: #'ExampleValue'
22
24
23
25
```tsx
24
26
function Todos() {
@@ -30,16 +32,13 @@ function Todos() {
30
32
}
31
33
```
32
34
33
-
[//]: #'Example'
35
+
[//]: #'ExampleValue'
34
36
[//]: #'Memoization'
35
37
36
38
### Placeholder Data Memoization
37
39
38
40
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:
`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).
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:
- 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.
154
154
-`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.
0 commit comments