Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/react/reference/useMutation.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mutate(variables, {
- This function will fire before the mutation function is fired and is passed the same variables the mutation function would receive
- Useful to perform optimistic updates to a resource in hopes that the mutation succeeds
- 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.
- `onSuccess: (data: TData, variables: TVariables, context?: TContext) => Promise<unknown> | unknown`
- `onSuccess: (data: TData, variables: TVariables, context: TContext) => Promise<unknown> | unknown`
- Optional
- This function will fire when the mutation is successful and will be passed the mutation's result.
- If a promise is returned, it will be awaited and resolved before proceeding
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export class Mutation<
this as Mutation<unknown, unknown, unknown, unknown>,
)

await this.options.onSuccess?.(data, variables, this.state.context)
await this.options.onSuccess?.(data, variables, this.state.context!)

// Notify cache callback
await this.#mutationCache.config.onSettled?.(
Expand Down
26 changes: 8 additions & 18 deletions packages/query-core/src/mutationObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,29 +144,19 @@ export class MutationObserver<
notifyManager.batch(() => {
// First trigger the mutate callbacks
if (this.#mutateOptions && this.hasListeners()) {
const variables = this.#currentResult.variables!
const context = this.#currentResult.context

if (action?.type === 'success') {
this.#mutateOptions.onSuccess?.(
action.data,
this.#currentResult.variables!,
this.#currentResult.context!,
)
this.#mutateOptions.onSettled?.(
action.data,
null,
this.#currentResult.variables!,
this.#currentResult.context,
)
this.#mutateOptions.onSuccess?.(action.data, variables, context!)
this.#mutateOptions.onSettled?.(action.data, null, variables, context)
} else if (action?.type === 'error') {
this.#mutateOptions.onError?.(
action.error,
this.#currentResult.variables!,
this.#currentResult.context,
)
this.#mutateOptions.onError?.(action.error, variables, context)
this.#mutateOptions.onSettled?.(
undefined,
action.error,
this.#currentResult.variables!,
this.#currentResult.context,
variables,
context,
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ export interface MutationOptions<
onSuccess?: (
data: TData,
variables: TVariables,
context: TContext | undefined,
context: TContext,
) => Promise<unknown> | unknown
onError?: (
error: TError,
Expand Down