Skip to content

feat: Add onSettled callback to commitMutation and useMutation#5304

Open
jungwoo3490 wants to merge 3 commits into
facebook:mainfrom
jungwoo3490:feat/add-on-settled-callback-to-mutations
Open

feat: Add onSettled callback to commitMutation and useMutation#5304
jungwoo3490 wants to merge 3 commits into
facebook:mainfrom
jungwoo3490:feat/add-on-settled-callback-to-mutations

Conversation

@jungwoo3490

@jungwoo3490 jungwoo3490 commented May 30, 2026

Copy link
Copy Markdown
Contributor

I added an onSettled callback to commitMutation and useMutation, similar to React Query's onSettled. It fires after both onCompleted and onError, removing the need to duplicate "always-run" logic across both callbacks (e.g. hiding a loading indicator, closing a modal, sending analytics).

Signature:

onSettled?: (
  response: TData | null,       // response data on success, null on error
  errors: PayloadError[] | null, // payload-level errors (can exist on success too)
  error: Error | null,           // network/execution error, null on success
) => void

Behavior:

Success: onCompleted(response, payloadErrors) -> onSettled(response, payloadErrors, null)
Error: onError(err) -> onSettled(null, null, err)

why invocation logic lives only in commitMutation

useMutation is a React wrapper around commitMutation. It intercepts onCompleted and onError solely to inject cleanup logic (in-flight state management). onSettled fires after that cleanup has already run, so there is nothing for useMutation to add.

Instead, useMutation passes onSettled through to commitMutation naturally via ...config spread, and commitMutation handles the invocation directly. This keeps the callback logic in one place and avoids duplicating it across both layers.

Test

  • commitMutation: success, success with payload errors, network error, onSettled-only (no onCompleted/onError)
  • useMutation: same 4 cases above

@meta-cla meta-cla Bot added the CLA Signed label May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant