Skip to content

Commit 75199ba

Browse files
committed
docs: add pages
1 parent a92d643 commit 75199ba

File tree

5 files changed

+204
-43
lines changed

5 files changed

+204
-43
lines changed

docs/src/manifests/manifest.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,21 @@
332332
"path": "/reference/QueryCache",
333333
"editUrl": "/reference/QueryCache.md"
334334
},
335+
{
336+
"title": "MutationCache",
337+
"path": "/reference/MutationCache",
338+
"editUrl": "/reference/MutationCache.md"
339+
},
335340
{
336341
"title": "QueryObserver",
337342
"path": "/reference/QueryObserver",
338343
"editUrl": "/reference/QueryObserver.md"
339344
},
345+
{
346+
"title": "InfiniteQueryObserver",
347+
"path": "/reference/InfiniteQueryObserver",
348+
"editUrl": "/reference/InfiniteQueryObserver.md"
349+
},
340350
{
341351
"title": "QueriesObserver",
342352
"path": "/reference/QueriesObserver",

docs/src/pages/guides/migrating-to-react-query-3.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function Todo() {
4545

4646
const onClickButton = useCallback(() => {
4747
queryClient.invalidateQueries('posts')
48-
}, [client])
48+
}, [queryClient])
4949

5050
return <button onClick={onClickButton}>Refetch</button>
5151
}
@@ -249,11 +249,11 @@ The `queryCache.resetErrorBoundaries()` method has been replaced by the `QueryEr
249249
250250
### queryCache.getQuery()
251251
252-
The `queryCache.getQuery()` method has been replaced by `cache.find()`.
252+
The `queryCache.getQuery()` method has been replaced by `queryCache.find()`.
253253
254254
### queryCache.getQueries()
255255
256-
The `queryCache.getQueries()` method has been replaced by `cache.findAll()`.
256+
The `queryCache.getQueries()` method has been replaced by `queryCache.findAll()`.
257257
258258
### queryCache.isFetching
259259
@@ -356,6 +356,18 @@ function Overview() {
356356
}
357357
```
358358
359+
## Retry/offline mutations
360+
361+
By default React Query will not retry a mutation on error, but it is possible with the `retry` option:
362+
363+
```js
364+
const mutation = useMutation(addTodo, {
365+
retry: 3,
366+
})
367+
```
368+
369+
If mutations fail because the device is offline, they will be retried in the same order when the device reconnects.
370+
359371
#### QueryObserver
360372
361373
A `QueryObserver` can be used to create and/or watch a query:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
id: InfiniteQueryObserver
3+
title: InfiniteQueryObserver
4+
---
5+
6+
## `InfiniteQueryObserver`
7+
8+
The `InfiniteQueryObserver` can be used to observe and switch between infinite queries.
9+
10+
```js
11+
const observer = new InfiniteQueryObserver(queryClient, {
12+
queryKey: 'posts',
13+
queryFn: fetchPosts,
14+
getNextPageParam: (lastPage, allPages) => lastPage.nextCursor,
15+
getPreviousPageParam: (firstPage, allPages) => firstPage.prevCursor,
16+
})
17+
18+
const unsubscribe = observer.subscribe(result => {
19+
console.log(result)
20+
unsubscribe()
21+
})
22+
```
23+
24+
**Options**
25+
26+
The options for the `InfiniteQueryObserver` are exactly the same as those of [`useInfiniteQuery`](#useinfinitequery).
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
id: MutationCache
3+
title: MutationCache
4+
---
5+
6+
The `MutationCache` is the storage for mutations.
7+
8+
**Normally, you will not interact with the MutationCache directly and instead use the `QueryClient`.**
9+
10+
```js
11+
import { MutationCache } from 'react-query'
12+
13+
const mutationCache = new MutationCache()
14+
```
15+
16+
Its available methods are:
17+
18+
- [`getAll`](#mutationcachegetall)
19+
- [`subscribe`](#mutationcachesubscribe)
20+
- [`clear`](#mutationcacheclear)
21+
22+
## `mutationCache.getAll`
23+
24+
`getAll` returns all mutations within the cache.
25+
26+
> Note: This is not typically needed for most applications, but can come in handy when needing more information about a mutation in rare scenarios
27+
28+
```js
29+
const mutations = mutationCache.getAll()
30+
```
31+
32+
**Returns**
33+
34+
- `Mutation[]`
35+
- Mutation instances from the cache
36+
37+
## `mutationCache.subscribe`
38+
39+
The `subscribe` method can be used to subscribe to the mutation cache as a whole and be informed of safe/known updates to the cache like mutation states changing or mutations being updated, added or removed.
40+
41+
```js
42+
const callback = mutation => {
43+
console.log(mutation)
44+
}
45+
46+
const unsubscribe = mutationCache.subscribe(callback)
47+
```
48+
49+
**Options**
50+
51+
- `callback: (mutation?: Mutation) => void`
52+
- This function will be called with the mutation cache any time it is updated.
53+
54+
**Returns**
55+
56+
- `unsubscribe: Function => void`
57+
- This function will unsubscribe the callback from the mutation cache.
58+
59+
## `mutationCache.clear`
60+
61+
The `clear` method can be used to clear the cache entirely and start fresh.
62+
63+
```js
64+
mutationCache.clear()
65+
```

docs/src/pages/reference/QueryClient.md

Lines changed: 88 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ title: QueryClient
88
The `QueryClient` can be used to interact with a cache:
99

1010
```js
11-
import { QueryClient, QueryCache } from 'react-query'
11+
import { QueryClient } from 'react-query'
1212

1313
const queryClient = new QueryClient({
1414
defaultOptions: {
@@ -28,22 +28,30 @@ Its available methods are:
2828
- [`getQueryData`](#queryclientgetquerydata)
2929
- [`setQueryData`](#queryclientsetquerydata)
3030
- [`getQueryState`](#queryclientgetquerystate)
31-
- [`setQueryDefaults`](#queryclientsetquerydefaults)
32-
- [`refetchQueries`](#queryclientrefetchqueries)
3331
- [`invalidateQueries`](#queryclientinvalidatequeries)
32+
- [`refetchQueries`](#queryclientrefetchqueries)
3433
- [`cancelQueries`](#queryclientcancelqueries)
3534
- [`removeQueries`](#queryclientremovequeries)
3635
- [`isFetching`](#queryclientisfetching)
3736
- [`getDefaultOptions`](#queryclientsetdefaultoptions)
3837
- [`setDefaultOptions`](#queryclientgetdefaultoptions)
38+
- [`getQueryDefaults`](#queryclientgetquerydefaults)
39+
- [`setQueryDefaults`](#queryclientsetquerydefaults)
40+
- [`getMutationDefaults`](#queryclientgetmutationdefaults)
41+
- [`setMutationDefaults`](#queryclientsetmutationdefaults)
42+
- [`getQueryCache`](#queryclientgetquerycache)
43+
- [`getMutationCache`](#queryclientgetmutationcache)
44+
- [`clear`](#queryclientclear)
3945

4046
**Options**
4147

42-
- `queryCache: QueryCache`
48+
- `queryCache?: QueryCache`
49+
- Optional
4350
- The query cache this client is connected to.
44-
- `mutationCache: MutationCache`
51+
- `mutationCache?: MutationCache`
52+
- Optional
4553
- The mutation cache this client is connected to.
46-
- `defaultOptions: DefaultOptions`
54+
- `defaultOptions?: DefaultOptions`
4755
- Optional
4856
- Define defaults for all queries and mutations using this queryClient.
4957

@@ -169,40 +177,6 @@ console.log(state.updatedAt)
169177
- `queryKey?: QueryKey`: [Query Keys](../guides/query-keys)
170178
- `filters?: QueryFilters`: [Query Filters](../guides/query-filters)
171179

172-
## `queryClient.setQueryDefaults`
173-
174-
`setQueryDefaults` is a synchronous method to set default options for specific queries:
175-
176-
```js
177-
queryClient.setQueryDefaults('posts', { queryFn: fetchPosts })
178-
179-
function Component() {
180-
const { data } = useQuery('posts')
181-
}
182-
```
183-
184-
**Options**
185-
186-
- `queryKey: QueryKey`: [Query Keys](../guides/query-keys)
187-
- `options: QueryOptions`
188-
189-
## `queryClient.setMutationDefaults`
190-
191-
`setMutationDefaults` is a synchronous method to set default options for specific mutations:
192-
193-
```js
194-
queryClient.setMutationDefaults('addPost', { mutationFn: addPost })
195-
196-
function Component() {
197-
const { data } = useMutation('addPost')
198-
}
199-
```
200-
201-
**Options**
202-
203-
- `mutationKey: string | unknown[]`
204-
- `options: MutationOptions`
205-
206180
## `queryClient.invalidateQueries`
207181

208182
The `invalidateQueries` method can be used to invalidate and refetch single or multiple queries in the cache based on their query keys or any other functionally accessible property/state of the query. By default, all matching queries are immediately marked as invalid and active queries are refetched in the background.
@@ -340,3 +314,77 @@ queryClient.setDefaultOptions({
340314
},
341315
})
342316
```
317+
318+
## `queryClient.getQueryDefaults`
319+
320+
The `getQueryDefaults` method returns the default options which have been set for specific queries:
321+
322+
```js
323+
const defaultOptions = queryClient.getQueryDefaults('posts')
324+
```
325+
326+
## `queryClient.setQueryDefaults`
327+
328+
`setQueryDefaults` can be used to set default options for specific queries:
329+
330+
```js
331+
queryClient.setQueryDefaults('posts', { queryFn: fetchPosts })
332+
333+
function Component() {
334+
const { data } = useQuery('posts')
335+
}
336+
```
337+
338+
**Options**
339+
340+
- `queryKey: QueryKey`: [Query Keys](../guides/query-keys)
341+
- `options: QueryOptions`
342+
343+
## `queryClient.getMutationDefaults`
344+
345+
The `getMutationDefaults` method returns the default options which have been set for specific mutations:
346+
347+
```js
348+
const defaultOptions = queryClient.getMutationDefaults('addPost')
349+
```
350+
351+
## `queryClient.setMutationDefaults`
352+
353+
`setMutationDefaults` can be used to set default options for specific mutations:
354+
355+
```js
356+
queryClient.setMutationDefaults('addPost', { mutationFn: addPost })
357+
358+
function Component() {
359+
const { data } = useMutation('addPost')
360+
}
361+
```
362+
363+
**Options**
364+
365+
- `mutationKey: string | unknown[]`
366+
- `options: MutationOptions`
367+
368+
## `queryClient.getQueryCache`
369+
370+
The `getQueryCache` method returns the query cache this client is connected to.
371+
372+
```js
373+
const queryCache = queryClient.getQueryCache()
374+
```
375+
376+
## `queryClient.getMutationCache`
377+
378+
The `getMutationCache` method returns the mutation cache this client is connected to.
379+
380+
```js
381+
const mutationCache = queryClient.getMutationCache()
382+
```
383+
384+
## `queryClient.clear`
385+
386+
The `clear` method clears all connected caches.
387+
388+
```js
389+
queryClient.clear()
390+
```

0 commit comments

Comments
 (0)