Skip to content

Commit 82a6345

Browse files
committed
📝 Add 'shared mutation results' documentation
1 parent 2de3d84 commit 82a6345

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

docs/rtk-query/api/created-api/hooks.mdx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ type UseMutation = (
315315
type UseMutationStateOptions = {
316316
// A method to determine the contents of `UseMutationResult`
317317
selectFromResult?: (result: UseMutationStateDefaultResult) => any
318+
// A string used to enable shared results across hook instances which have the same key
319+
fixedCacheKey?: string
318320
}
319321

320322
type UseMutationTrigger<T> = (arg: any) => Promise<
@@ -328,7 +330,7 @@ type UseMutationTrigger<T> = (arg: any) => Promise<
328330

329331
type UseMutationResult<T> = {
330332
// 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
332334
data?: T // Returned result if present
333335
error?: unknown // Error result if present
334336
endpointName?: string // The name of the given endpoint for the mutation
@@ -359,7 +361,9 @@ selectFromResult: () => ({})
359361
360362
- **Parameters**
361363
362-
- `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
363367
364368
- **Returns**: A tuple containing:
365369
- `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

docs/rtk-query/usage/mutations.mdx

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,63 @@ With RTK Query, a mutation does not contain a semantic distinction between 'load
117117

118118
:::
119119

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+
export const ComponentOne = () => {
128+
// Triggering `updatePostOne` will affect the result in this component,
129+
// but not the result in `ComponentTwo`, and vice-versa
130+
const [updatePost, result] = useUpdatePostMutation()
131+
132+
return <div>...</div>
133+
}
134+
135+
export const ComponentTwo = () => {
136+
const [updatePost, result] = useUpdatePostMutation()
137+
138+
return <div>...</div>
139+
}
140+
```
141+
142+
RTK Query provides an option to share results across mutation hook instances using the
143+
`fixedCacheKey` option.
144+
Any `useMutation` hooks with the same `fixedCacheKey` string will share results between each other
145+
when any of the trigger functions are called. This should be a unique string shared between each
146+
mutation hook instance you wish to share results.
147+
148+
```tsx no-transpile
149+
export const ComponentOne = () => {
150+
// Triggering `updatePostOne` will affect the result in both this component,
151+
// but as well as the result in `ComponentTwo`, and vice-versa
152+
const [updatePost, result] = useUpdatePostMutation({
153+
fixedCacheKey: 'shared-update-post',
154+
})
155+
156+
return <div>...</div>
157+
}
158+
159+
export const ComponentTwo = () => {
160+
const [updatePost, result] = useUpdatePostMutation({
161+
fixedCacheKey: 'shared-update-post',
162+
})
163+
164+
return <div>...</div>
165+
}
166+
```
167+
168+
:::note
169+
170+
When using `fixedCacheKey`, the `originalArgs` property is not able to be shared and will always be `undefined`.
171+
172+
:::
173+
120174
### Standard Mutation Example
121175

122-
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.
123177

124178
```tsx title="src/features/posts/PostDetail.tsx"
125179
export const PostDetail = () => {

0 commit comments

Comments
 (0)