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
requestId:string// A string generated by RTK Query
324
326
abort: () =>void// A method to cancel the mutation promise
325
327
unwrap: () =>Promise<T> // A method to unwrap the mutation call and provide the raw response/error
326
-
unsubscribe: () =>void// A method to manually unsubscribe from the mutation call
328
+
reset: () =>void// A method to manually unsubscribe from the mutation call and reset the result to the uninitialized state
327
329
}
328
330
329
331
typeUseMutationResult<T> = {
330
332
// Base query state
331
-
originalArgs?:unknown// Arguments passed to the latest mutation call
333
+
originalArgs?:unknown// Arguments passed to the latest mutation call. Not available if using the `fixedCacheKey` option
332
334
data?:T// Returned result if present
333
335
error?:unknown// Error result if present
334
336
endpointName?:string// The name of the given endpoint for the mutation
@@ -340,6 +342,8 @@ type UseMutationResult<T> = {
340
342
isSuccess:boolean// Mutation has data from a successful call
341
343
isError:boolean// Mutation is currently in an "error" state
342
344
startedTimeStamp?:number// Timestamp for when the latest mutation was initiated
345
+
346
+
reset: () =>void// A method to manually unsubscribe from the mutation call and reset the result to the uninitialized state
343
347
}
344
348
```
345
349
@@ -357,7 +361,9 @@ selectFromResult: () => ({})
357
361
358
362
- **Parameters**
359
363
360
-
- `options`: A set of options that control the subscription behavior of the hook
364
+
- `options`: A set of options that control the subscription behavior of the hook:
365
+
- `selectFromResult`: A callback that can be used to customize the mutation result returned as the second item in the tuple
366
+
- `fixedCacheKey`: An optional string used to enable shared results across hook instances
361
367
362
368
- **Returns**: A tuple containing:
363
369
- `trigger`: A function that triggers an update to the data based on the provided argument. The trigger function returns a promise with the properties shown above that may be used to handle the behavior of the promise
Copy file name to clipboardExpand all lines: docs/rtk-query/usage/mutations.mdx
+56-1Lines changed: 56 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -109,16 +109,71 @@ Below are some of the most frequently used properties on the "mutation result" o
109
109
-`isLoading` - When true, indicates that the mutation has been fired and is awaiting a response.
110
110
-`isSuccess` - When true, indicates that the last mutation fired has data from a successful request.
111
111
-`isError` - When true, indicates that the last mutation fired resulted in an error state.
112
+
-`reset` - A method to reset the hook back to it's original state and remove the current result from the cache
112
113
113
114
:::note
114
115
115
116
With RTK Query, a mutation does not contain a semantic distinction between 'loading' and 'fetching' in the way that a [query does](./queries.mdx#frequently-used-query-hook-return-values). For a mutation, subsequent calls are not assumed to be necessarily related, so a mutation is either 'loading' or 'not loading', with no concept of 're-fetching'.
116
117
117
118
:::
118
119
120
+
### Shared Mutation Results
121
+
122
+
By default, separate instances of a `useMutation` hook are not inherently related to each other.
123
+
Triggering one instance will not affect the result for a separate instance. This applies regardless
124
+
of whether the hooks are called within the same component, or different components.
125
+
126
+
```tsx no-transpile
127
+
exportconst ComponentOne = () => {
128
+
// Triggering `updatePostOne` will affect the result in this component,
129
+
// but not the result in `ComponentTwo`, and vice-versa
When using `fixedCacheKey`, the `originalArgs` property is not able to be shared and will always be `undefined`.
171
+
172
+
:::
173
+
119
174
### Standard Mutation Example
120
175
121
-
This is a modified version of the complete example you can see at the bottom of the page to highlight the `updatePost` mutation. In this scenario, a post is fetched with `useQuery`, and then a`EditablePostName` component is rendered that allows us to edit the name of the post.
176
+
This is a modified version of the complete example you can see at the bottom of the page to highlight the `updatePost` mutation. In this scenario, a post is fetched with `useQuery`, and then an`EditablePostName` component is rendered that allows us to edit the name of the post.
0 commit comments