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/src/pages/guides/initial-query-data.md
+16-6Lines changed: 16 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,19 @@ id: initial-query-data
3
3
title: Initial Query Data
4
4
---
5
5
6
-
## Initial Data
6
+
There are many ways to supply initial data for a query to the cache before you need it:
7
7
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`
9
19
10
20
```js
11
21
functionTodos() {
@@ -15,7 +25,7 @@ function Todos() {
15
25
}
16
26
```
17
27
18
-
##Using Initial Data with`staleTime`
28
+
### Initial Data and`staleTime`
19
29
20
30
`initialData` is treated exactly the same as normal data, which means that is follows the same rules and expectations of `staleTime`.
21
31
@@ -32,9 +42,9 @@ function Todos() {
32
42
33
43
> 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
34
44
35
-
## Initial Data Function
45
+
###Initial Data Function
36
46
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:
38
48
39
49
```js
40
50
functionTodos() {
@@ -46,7 +56,7 @@ function Todos() {
46
56
}
47
57
```
48
58
49
-
## Initial Data from Cache
59
+
###Initial Data from Cache
50
60
51
61
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:
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)
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:
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:
0 commit comments