Skip to content

Commit 42dd010

Browse files
committed
docs(guides and examples): improve and fix typo
1 parent 1cb7065 commit 42dd010

File tree

7 files changed

+95
-45
lines changed

7 files changed

+95
-45
lines changed

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

Lines changed: 89 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,46 @@ const result = useQuery('todos', () => fetch('/todos'), {
2323
})
2424
```
2525

26-
### Initial Data and `staleTime`
27-
28-
`initialData` is treated exactly the same as normal data, which means that is follows the same rules and expectations of `staleTime`.
29-
30-
- If you configure your query observer with a `staleTime` of `10000`, for example, the `initialData` you provide will be considered fresh for that same amount of time, just like your normal data.
31-
32-
```js
33-
const result = useQuery('todos', () => fetch('/todos'), {
34-
initialData: initialTodos,
35-
staleTime: 10000,
36-
})
37-
```
26+
### `staleTime` and `initialDataUpdatedAt`
27+
28+
By default, `initialData` is treated as totally fresh, as if it were just fetched. This also means that it will affect how it is interpreted by the `staleTime` option.
29+
30+
- If you configure your query observer with `initialData`, and no `staleTime` (the default `staleTime: 0`), the query will immediately refetch when it mounts:
31+
32+
```js
33+
function Todos() {
34+
// Will show initialTodos immediately, but also immediately refetch todos after mount
35+
return useQuery('todos', () => fetch('/todos'), {
36+
initialData: initialTodos,
37+
})
38+
}
39+
```
40+
41+
- If you configure your query observer with `initialData` and a `staleTime` of `1000` ms, the data will be considered fresh for that same amount of time, as if it was just fetched from your query function.
42+
43+
```js
44+
function Todos() {
45+
// Show initialTodos immeidately, but won't refetch until another interaction event is encountered after 1000 ms
46+
return useQuery('todos', () => fetch('/todos'), {
47+
initialData: initialTodos,
48+
staleTime: 1000,
49+
})
50+
}
51+
```
52+
53+
- So what if your `initialData` isn't totally fresh? That leaves us with the last configuration that is actually the most accurate and uses an option called `initialDataUpdatedAt`. This options allows you to pass a numeric JS timestamp in milliseconds of when the initialData itself was last updated, e.g. what `Date.now()` provides. Take note that if you have a unix timestamp, you'll need to convert it to a JS timestamp by multiplying it by `1000`.
54+
```js
55+
function Todos() {
56+
// Show initialTodos immeidately, but won't refetch until another interaction event is encountered after 1000 ms
57+
return useQuery('todos', () => fetch('/todos'), {
58+
initialData: initialTodos,
59+
staleTime: 60 * 1000 // 1 minute
60+
// This could be 10 seconds ago or 10 minutes ago
61+
initialDataUpdatedAt: initialTodosUpdatedTimestamp // eg. 1608412420052
62+
})
63+
}
64+
```
65+
This option allows the staleTime to be used for it's original purpose, determining how fresh the data needs to be, while also allowing the data to be refetched on mount if the `initialData` is older than the `staleTime`. In the example above, our data needs to be fresh within 1 minute, and we can hint to the query when the initialData was last updated so the query can decide for itself whether the data needs to be refetched again or not.
3866

3967
> 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
4068
@@ -43,42 +71,64 @@ const result = useQuery('todos', () => fetch('/todos'), {
4371
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:
4472

4573
```js
46-
const result = useQuery('todos', () => fetch('/todos'), {
47-
initialData: () => {
48-
return getExpensiveTodos()
49-
},
50-
})
51-
74+
function Todos() {
75+
return useQuery('todos', () => fetch('/todos'), {
76+
initialData: () => {
77+
return getExpensiveTodos()
78+
},
79+
})
80+
}
5281
```
5382

5483
### Initial Data from Cache
5584

5685
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:
5786

5887
```js
59-
const result = useQuery(['todo', todoId], () => fetch('/todos'), {
60-
initialData: () => {
61-
// Use a todo from the 'todos' query as the initial data for this todo query
62-
return queryClient.getQueryData('todos')?.find(d => d.id === todoId)
63-
},
64-
})
88+
function Todo({ todoId }) {
89+
return useQuery(['todo', todoId], () => fetch('/todos'), {
90+
initialData: () => {
91+
// Use a todo from the 'todos' query as the initial data for this todo query
92+
return queryClient.getQueryData('todos')?.find(d => d.id === todoId)
93+
},
94+
})
95+
}
6596
```
6697
67-
Most of the time, this pattern works well, but if the source query you're using to look up the initial data from is old, you may not want to use the data at all and just fetch from the server. To make this decision easier, you can use the `queryClient.getQueryState` method instead to get more information about the source query, including a `state.dataUpdatedAt` timestamp you can use to decide if the query is "fresh" enough for your needs:
98+
### Initial Data from the cache with `initialDataUpdatedAt`
99+
100+
Getting initial data from the cache means the source query you're using to look up the initial data from is likely old, but `initialData` Instead of using an artificial `staleTime` to keep your query from refetching immediately, it's suggested that you pass the source query's `dataUpdatedAt` to `initialDataUpdatedAt`. This provides the query instance with all the information it needs to determine if and when the query needs to be refetched, regardless of initial data being provided.
68101
69102
```js
70-
const result = useQuery(['todo', todoId], () => fetch('/todos'), {
71-
initialData: () => {
72-
// Get the query state
73-
const state = queryClient.getQueryState('todos')
74-
75-
// If the query exists and has data that is no older than 10 seconds...
76-
if (state && Date.now() - state.dataUpdatedAt <= 10 * 1000) {
77-
// return the individual todo
78-
return state.data.find(d => d.id === todoId)
79-
}
80-
81-
// Otherwise, return undefined and let it fetch!
82-
},
83-
})
103+
function Todo({ todoId }) {
104+
return useQuery(['todo', todoId], () => fetch('/todos'), {
105+
initialData: () =>
106+
queryClient.getQueryData('todos')?.find(d => d.id === todoId),
107+
initialDataUpdatedAt: () =>
108+
queryClient.getQueryState('todos')?.dataUpdatedAt,
109+
})
110+
}
84111
```
112+
113+
### Conditional Initial Data from Cache
114+
115+
If the source query you're using to look up the initial data from is old, you may not want to use the cached data at all and just fetch from the server. To make this decision easier, you can use the `queryClient.getQueryState` method instead to get more information about the source query, including a `state.dataUpdatedAt` timestamp you can use to decide if the query is "fresh" enough for your needs:
116+
117+
```js
118+
function Todo({ todoId }) {
119+
return useQuery(['todo', todoId], () => fetch('/todos'), {
120+
initialData: () => {
121+
// Get the query state
122+
const state = queryClient.getQueryState('todos')
123+
124+
// If the query exists and has data that is no older than 10 seconds...
125+
if (state && Date.now() - state.dataUpdatedAt <= 10 * 1000) {
126+
// return the individual todo
127+
return state.data.find(d => d.id === todoId)
128+
}
129+
130+
// Otherwise, return undefined and let it fetch from a hard loading state!
131+
},
132+
})
133+
}
134+
```

docs/src/pages/guides/mutations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ In the example above, you also saw that you can pass variables to your mutations
5050

5151
Even with just variables, mutations aren't all that special, but when used with the `onSuccess` option, the [Query Client's `invalidateQueries` method](../reference/QueryClient#queryclientinvalidatequeries) and the [Query Client's `setQueryData` method](../reference/QueryClient#queryclientsetquerydata), mutations become a very powerful tool.
5252

53-
> IMPORTANT: The `mutate` function is an asynchronous function, which means you cannot use it directly in an event callback. If you need to access the event in `onSubmit` you need to wrap `mutate` in another function. This is due to [React event pooling](https://reactjs.org/docs/events.html#event-pooling).
53+
> IMPORTANT: The `mutate` function is an asynchronous function, which means you cannot use it directly in an event callback. If you need to access the event in `on:submit` you need to wrap `mutate` in another function.
5454
5555
```markdown
5656
<script>

docs/src/pages/guides/query-invalidation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ When a query is invalidated with `invalidateQueries`, two things happen:
2121

2222
## Query Matching with `invalidateQueries`
2323

24-
When using APIs like `invalidateQueries` and `removeQueries` (and others that support partial query matching), you can match multiple queries by their prefix, or get really specific and match an exact query. For informationo on the types of filters, you can use, please see [Query Filters](./query-filters).
24+
When using APIs like `invalidateQueries` and `removeQueries` (and others that support partial query matching), you can match multiple queries by their prefix, or get really specific and match an exact query. For information on the types of filters, you can use, please see [Query Filters](./query-filters).
2525

2626
In this example, we can use the `todos` prefix to invalidate any queries that start with `todos` in their query key:
2727

docs/src/pages/reference/focusManager.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: FocusManager
33
title: FocusManager
44
---
55

6-
The `FocusManager` manages the focus state within React Query.
6+
The `FocusManager` manages the focus state within Svelte Query.
77

88
It can be used to change the default event listeners or to manually change the focus state.
99

docs/src/pages/reference/onlineManager.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: OnlineManager
33
title: OnlineManager
44
---
55

6-
The `OnlineManager` manages the online state within React Query.
6+
The `OnlineManager` manages the online state within Svelte Query.
77

88
It can be used to change the default event listeners or to manually change the online state.
99

examples/custom-hooks/src/components/BasicQuery.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<p>
1212
This example is exactly the same as the basic example, but each query has been
1313
refactored to be it's own custom hook. This design is the suggested way to use
14-
React Query, as it makes it much easier to manage query keys and shared query
14+
Svelte Query, as it makes it much easier to manage query keys and shared query
1515
logic.
1616
</p>
1717
{#if postId > -1}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@sveltestack/svelte-query",
33
"private": false,
4-
"version": "1.0.0",
4+
"version": "1.0.1",
55
"description": "Hooks for managing, caching and syncing asynchronous and remote data in Svelte",
66
"license": "MIT",
77
"svelte": "svelte/index.js",

0 commit comments

Comments
 (0)