Skip to content

Commit 320cf9b

Browse files
Mini-ghostTkDodo
andauthored
fix(query-core): fix context type error in onSuccess (#6355)
* fix(query-core): fix context type error in `onSuccess` * docs(query-core): update `useMutation` documentations * fix(query-core): the `context` should not be inferred as `undefined` when `TContext` is defined --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent c38c1cb commit 320cf9b

File tree

4 files changed

+11
-21
lines changed

4 files changed

+11
-21
lines changed

docs/react/reference/useMutation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ mutate(variables, {
6363
- This function will fire before the mutation function is fired and is passed the same variables the mutation function would receive
6464
- Useful to perform optimistic updates to a resource in hopes that the mutation succeeds
6565
- The value returned from this function will be passed to both the `onError` and `onSettled` functions in the event of a mutation failure and can be useful for rolling back optimistic updates.
66-
- `onSuccess: (data: TData, variables: TVariables, context?: TContext) => Promise<unknown> | unknown`
66+
- `onSuccess: (data: TData, variables: TVariables, context: TContext) => Promise<unknown> | unknown`
6767
- Optional
6868
- This function will fire when the mutation is successful and will be passed the mutation's result.
6969
- If a promise is returned, it will be awaited and resolved before proceeding

packages/query-core/src/mutation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export class Mutation<
218218
this as Mutation<unknown, unknown, unknown, unknown>,
219219
)
220220

221-
await this.options.onSuccess?.(data, variables, this.state.context)
221+
await this.options.onSuccess?.(data, variables, this.state.context!)
222222

223223
// Notify cache callback
224224
await this.#mutationCache.config.onSettled?.(

packages/query-core/src/mutationObserver.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,19 @@ export class MutationObserver<
144144
notifyManager.batch(() => {
145145
// First trigger the mutate callbacks
146146
if (this.#mutateOptions && this.hasListeners()) {
147+
const variables = this.#currentResult.variables!
148+
const context = this.#currentResult.context
149+
147150
if (action?.type === 'success') {
148-
this.#mutateOptions.onSuccess?.(
149-
action.data,
150-
this.#currentResult.variables!,
151-
this.#currentResult.context!,
152-
)
153-
this.#mutateOptions.onSettled?.(
154-
action.data,
155-
null,
156-
this.#currentResult.variables!,
157-
this.#currentResult.context,
158-
)
151+
this.#mutateOptions.onSuccess?.(action.data, variables, context!)
152+
this.#mutateOptions.onSettled?.(action.data, null, variables, context)
159153
} else if (action?.type === 'error') {
160-
this.#mutateOptions.onError?.(
161-
action.error,
162-
this.#currentResult.variables!,
163-
this.#currentResult.context,
164-
)
154+
this.#mutateOptions.onError?.(action.error, variables, context)
165155
this.#mutateOptions.onSettled?.(
166156
undefined,
167157
action.error,
168-
this.#currentResult.variables!,
169-
this.#currentResult.context,
158+
variables,
159+
context,
170160
)
171161
}
172162
}

packages/query-core/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ export interface MutationOptions<
671671
onSuccess?: (
672672
data: TData,
673673
variables: TVariables,
674-
context: TContext | undefined,
674+
context: TContext,
675675
) => Promise<unknown> | unknown
676676
onError?: (
677677
error: TError,

0 commit comments

Comments
 (0)