Skip to content

Commit b9bc663

Browse files
committed
Merge branch 'master' into feature/useIsMutating-overloads
2 parents c6c292c + 51a226e commit b9bc663

File tree

19 files changed

+520
-27
lines changed

19 files changed

+520
-27
lines changed

docs/src/pages/guides/caching.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ Let's assume we are using the default `cacheTime` of **5 minutes** and the defau
2121
- It will then cache the data using `'todos'` and `fetchTodos` as the unique identifiers for that cache.
2222
- The hook will mark itself as stale after the configured `staleTime` (defaults to `0`, or immediately).
2323
- A second instance of `useQuery('todos', fetchTodos)` mounts elsewhere.
24-
- Because this exact data exist in the cache from the first instance of this query, that data is immediately returned from the cache.
24+
- Because this exact data exists in the cache from the first instance of this query, that data is immediately returned from the cache.
2525
- A background refetch is triggered for both queries (but only one request), since a new instance appeared on screen.
2626
- Both instances are updated with the new data if the fetch is successful
2727
- Both instances of the `useQuery('todos', fetchTodos)` query are unmounted and no longer in use.
28-
- Since there are no more active instances to this query, a cache timeout is set using `cacheTime` to delete and garbage collect the query (defaults to **5 minutes**).
28+
- Since there are no more active instances of this query, a cache timeout is set using `cacheTime` to delete and garbage collect the query (defaults to **5 minutes**).
2929
- Before the cache timeout has completed another instance of `useQuery('todos', fetchTodos)` mounts. The query immediately returns the available cached value while the `fetchTodos` function is being run in the background to populate the query with a fresh value.
3030
- The final instance of `useQuery('todos', fetchTodos)` unmounts.
3131
- No more instances of `useQuery('todos', fetchTodos)` appear within **5 minutes**.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ By default, `initialData` is treated as totally fresh, as if it were just fetche
6464
})
6565
}
6666
```
67-
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.
67+
This option allows the staleTime to be used for its 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.
6868

6969
> 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
7070

docs/src/pages/guides/mutations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ A mutation can only be in one of the following states at any given moment:
4646

4747
Beyond those primary states, more information is available depending on the state of the mutation:
4848

49-
- `error` - If the mutation is in an `isError` state, the error is available via the `error` property.
49+
- `error` - If the mutation is in an `error` state, the error is available via the `error` property.
5050
- `data` - If the mutation is in a `success` state, the data is available via the `data` property.
5151

5252
In the example above, you also saw that you can pass variables to your mutations function by calling the `mutate` function with a **single variable or object**.

docs/src/pages/guides/testing.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ Note that we provide a custom wrapper that builds the `QueryClient` and `QueryCl
4646

4747
It is possible to write this wrapper only once, but if so we need to ensure that the `QueryClient` gets cleared before every test, and that tests don't run in parallel otherwise one test will influence the results of others.
4848

49+
## Turn off retries
50+
51+
The library defaults to three retries with exponential backoff, which means that your tests are likely to timeout if you want to test an erroneous query. The easiest way to turn retries off is via the QueryClientProvider. Let's extend the above example:
52+
53+
```
54+
const queryClient = new QueryClient({
55+
defaultOptions: {
56+
queries: {
57+
// ✅ turns retries off
58+
retry: false,
59+
},
60+
},
61+
})
62+
const wrapper = ({ children }) => (
63+
<QueryClientProvider client={queryClient}>
64+
{children}
65+
</QueryClientProvider>
66+
);
67+
```
68+
69+
This will set the defaults for all queries in the component tree to "no retries". It is important to know that this will only work if your actual useQuery has no explicit retries set. If you have a query that wants 5 retries, this will still take precedence, because defaults are only taken as a fallback.
70+
4971
## Testing Network Calls
5072

5173
The primary use for React Query is to cache network requests, so it's important that we can test our code is making the correct network requests in the first place.

docs/src/pages/plugins/createAsyncStoragePersistor.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This utility comes packaged with `react-query` and is available under the `react
1717
- Pass it to the [`persistQueryClient`](../persistQueryClient) function
1818

1919
```ts
20-
import AsyncStorage from '@react-native-community/async-storage'
20+
import AsyncStorage from '@react-native-async-storage/async-storage'
2121
import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
2222
import { createAsyncStoragePersistor } from 'react-query/createAsyncStoragePersistor-experimental'
2323

@@ -56,7 +56,7 @@ interface CreateAsyncStoragePersistorOptions {
5656
/** The storage client used for setting an retrieving items from cache */
5757
storage: AsyncStorage
5858
/** The key to use when storing the cache to localstorage */
59-
asyncStorageKey?: string
59+
key?: string
6060
/** To avoid localstorage spamming,
6161
* pass a time in ms to throttle saving the cache to disk */
6262
throttleTime?: number
@@ -73,7 +73,7 @@ The default options are:
7373

7474
```js
7575
{
76-
asyncStorageKey = `REACT_QUERY_OFFLINE_CACHE`,
76+
key = `REACT_QUERY_OFFLINE_CACHE`,
7777
throttleTime = 1000,
7878
}
7979
```

docs/src/pages/reference/MutationCache.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const mutationCache = new MutationCache({
1414
onError: error => {
1515
console.log(error)
1616
},
17+
onSuccess: data => {
18+
console.log(data)
19+
}
1720
})
1821
```
1922

@@ -28,6 +31,14 @@ Its available methods are:
2831
- `onError?: (error: unknown, variables: unknown, context: unknown, mutation: Mutation) => void`
2932
- Optional
3033
- This function will be called if some mutation encounters an error.
34+
- `onSuccess?: (data: unknown, variables: unknown, context: unknown, mutation: Mutation) => void`
35+
- Optional
36+
- This function will be called if some mutation is successful.
37+
38+
## Global callbacks
39+
40+
The `onError` and `onSuccess` callbacks on the MutationCache can be used to handle these events on a global level. They are different to `defaultOptions` provided to the QueryClient because:
41+
- `defaultOptions` can be overridden by each Mutation - the global callbacks will **always** be called.
3142

3243
## `mutationCache.getAll`
3344

docs/src/pages/reference/QueryCache.md

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

6-
The `QueryCache` is the storage mechanism for React Query. It stores all of the data, meta information and state of queries it contains.
6+
The `QueryCache` is the storage mechanism for React Query. It stores all the data, meta information and state of queries it contains.
77

88
**Normally, you will not interact with the QueryCache directly and instead use the `QueryClient` for a specific cache.**
99

@@ -14,6 +14,9 @@ const queryCache = new QueryCache({
1414
onError: error => {
1515
console.log(error)
1616
},
17+
onSuccess: data => {
18+
console.log(data)
19+
}
1720
})
1821

1922
const query = queryCache.find('posts')
@@ -31,6 +34,15 @@ Its available methods are:
3134
- `onError?: (error: unknown, query: Query) => void`
3235
- Optional
3336
- This function will be called if some query encounters an error.
37+
- `onSuccess?: (data: unknown, query: Query) => void`
38+
- Optional
39+
- This function will be called if some query is successful.
40+
41+
## Global callbacks
42+
43+
The `onError` and `onSuccess` callbacks on the QueryCache can be used to handle these events on a global level. They are different to `defaultOptions` provided to the QueryClient because:
44+
- `defaultOptions` can be overridden by each Query - the global callbacks will **always** be called.
45+
- `defaultOptions` callbacks will be called once for each Observer, while the global callbacks will only be called once per Query.
3446

3547
## `queryCache.find`
3648

docs/src/pages/reference/hydration/dehydrate.md

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

6-
`dehydrate` creates a frozen representation of a `cache` that can later be hydrated with `Hydrate`, `useHydrate`, or `hydrate`. This is useful for passing prefetched queries from server to client or persisting queries to localstorage or other persisten locations. It only includes currently successful queries by default.
6+
`dehydrate` creates a frozen representation of a `cache` that can later be hydrated with `Hydrate`, `useHydrate`, or `hydrate`. This is useful for passing prefetched queries from server to client or persisting queries to localstorage or other persistent locations. It only includes currently successful queries by default.
77

88
```js
99
import { dehydrate } from 'react-query/hydration'

media/logo.sketch

15.5 MB
Binary file not shown.

src/core/mutation.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ export class Mutation<
156156
.then(() => this.executeMutation())
157157
.then(result => {
158158
data = result
159+
// Notify cache callback
160+
this.mutationCache.config.onSuccess?.(
161+
data,
162+
this.state.variables,
163+
this.state.context,
164+
this as Mutation<unknown, unknown, unknown, unknown>
165+
)
159166
})
160167
.then(() =>
161168
this.options.onSuccess?.(
@@ -178,14 +185,12 @@ export class Mutation<
178185
})
179186
.catch(error => {
180187
// Notify cache callback
181-
if (this.mutationCache.config.onError) {
182-
this.mutationCache.config.onError(
183-
error,
184-
this.state.variables,
185-
this.state.context,
186-
this as Mutation<unknown, unknown, unknown, unknown>
187-
)
188-
}
188+
this.mutationCache.config.onError?.(
189+
error,
190+
this.state.variables,
191+
this.state.context,
192+
this as Mutation<unknown, unknown, unknown, unknown>
193+
)
189194

190195
// Log error
191196
getLogger().error(error)

0 commit comments

Comments
 (0)