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/infinite-queries.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -88,7 +88,7 @@ function Projects() {
88
88
89
89
## What happens when an infinite query needs to be refetched?
90
90
91
-
When an infinite query becomes `stale` and needs to be refetched, each group is fetched `sequentially`, starting from the first one. This ensures that even if the underlying data is mutated we're not using stale cursors and potentially getting duplicates or skipping records. If an infinite query's results are ever removed from the cache, the pagination restarts at the initial state with only the initial group being requested.
91
+
When an infinite query becomes `stale` and needs to be refetched, each group is fetched `sequentially`, starting from the first one. This ensures that even if the underlying data is mutated we're not using stale cursors and potentially getting duplicates or skipping records. If an infinite query's results are ever removed from the queryCache, the pagination restarts at the initial state with only the initial group being requested.
92
92
93
93
## What if I need to pass custom information to my query function?
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 `client.getQueryState` method instead to get more information about the source query, including a `state.updatedAt` timestamp you can use to decide if the query is "fresh" enough for your needs:
64
+
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.updatedAt` timestamp you can use to decide if the query is "fresh" enough for your needs:
Copy file name to clipboardExpand all lines: docs/src/pages/guides/migrating-to-react-query-3.md
+49-23Lines changed: 49 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,8 +9,8 @@ This article explains how to migrate your application to React Query 3.
9
9
10
10
### QueryClient
11
11
12
-
The `QueryCache` has been split into a `QueryClient`and a `QueryCache`.
13
-
The `QueryCache` contains all cached queriesand the `QueryClient` can be used to interact with a cache.
12
+
The `QueryCache` has been split into a `QueryClient`, `QueryCache`and `MutationCache`.
13
+
The `QueryCache` contains all queries, the `MutationCache` contains all mutations, and the `QueryClient` can be used to set configuration and to interact with them.
14
14
15
15
This has some benefits:
16
16
@@ -25,11 +25,17 @@ Use the `QueryClientProvider` component to connect a `QueryClient` to your appli
Copy file name to clipboardExpand all lines: docs/src/pages/guides/mutations.md
+26-22Lines changed: 26 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,7 @@ Beyond those primary state, more information is available depending on the state
51
51
52
52
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**.
53
53
54
-
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#clientinvalidatequeries) and the [Query Client's `setQueryData` method](../reference/QueryClient#clientsetquerydata), mutations become a very powerful tool.
54
+
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.
55
55
56
56
> 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).
57
57
@@ -120,18 +120,12 @@ useMutation(addTodo, {
120
120
onMutate:variables=> {
121
121
// A mutation is about to happen!
122
122
123
-
// Optionally return a context object with a rollback function
124
-
return {
125
-
rollback: () => {
126
-
// do some rollback logic
127
-
},
128
-
}
123
+
// Optionally return a context containing data to use when for example rolling back
124
+
return { id:1 }
129
125
},
130
126
onError: (error, variables, context) => {
131
127
// An error happened!
132
-
if (context.rollback) {
133
-
context.rollback()
134
-
}
128
+
console.log(`rolling back optimistic update with id ${context.id}`)
135
129
},
136
130
onSuccess: (data, variables, context) => {
137
131
// Boom baby!
@@ -155,41 +149,37 @@ useMutation(addTodo, {
155
149
})
156
150
```
157
151
158
-
You might find that you want to **add additional side-effects** to some of the `useMutation` lifecycle at the time of calling `mutate`. To do that, you can provide any of the same callback options to the `mutate` function after your mutation variable. Supported option overrides include:
159
-
160
-
-`onSuccess` - Will be fired after the `useMutation`-level `onSuccess` handler
161
-
-`onError` - Will be fired after the `useMutation`-level `onError` handler
162
-
-`onSettled` - Will be fired after the `useMutation`-level `onSettled` handler
152
+
You might find that you want to **trigger different callbacks** then the ones defined on `useMutation` when calling `mutate`. To do that, you can provide any of the same callback options to the `mutate` function after your mutation variable. Supported overrides include: `onSuccess`, `onError` and `onSettled`.
163
153
164
154
```js
165
155
useMutation(addTodo, {
166
156
onSuccess: (data, variables, context) => {
167
-
// I will fire first
157
+
// I will not fire
168
158
},
169
159
onError: (error, variables, context) => {
170
-
// I will fire first
160
+
// I will not fire
171
161
},
172
162
onSettled: (data, error, variables, context) => {
173
-
// I will fire first
163
+
// I will not fire
174
164
},
175
165
})
176
166
177
167
mutate(todo, {
178
168
onSuccess: (data, variables, context) => {
179
-
// I will fire second!
169
+
// I will fire instead!
180
170
},
181
171
onError: (error, variables, context) => {
182
-
// I will fire second!
172
+
// I will fire instead!
183
173
},
184
174
onSettled: (data, error, variables, context) => {
185
-
// I will fire second!
175
+
// I will fire instead!
186
176
},
187
177
})
188
178
```
189
179
190
180
## Promises
191
181
192
-
Use `mutateAsync` instead of `mutate` to get a promise which will resolve on success or throw on an error:
182
+
Use `mutateAsync` instead of `mutate` to get a promise which will resolve on success or throw on an error. This can for example be used to compose side effects.
193
183
194
184
```js
195
185
constmutation=useMutation(addTodo)
@@ -199,5 +189,19 @@ try {
199
189
console.log(todo)
200
190
} catch (error) {
201
191
console.error(error)
192
+
} finally {
193
+
console.log('done')
202
194
}
203
195
```
196
+
197
+
## Retry
198
+
199
+
By default React Query will not retry a mutation on error, but it is possible with the `retry` option:
200
+
201
+
```js
202
+
constmutation=useMutation(addTodo, {
203
+
retry:3,
204
+
})
205
+
```
206
+
207
+
If mutations fail because the device is offline, they will be retried in the same order when the device reconnects.
0 commit comments