Skip to content
Discussion options

You must be logged in to vote

Yes. In current TanStack Query v5, mutation callbacks receive a MutationFunctionContext as their final argument, and its client field is the QueryClient that owns the mutation:

const options = mutationOptions({
  mutationFn: saveTodo,
  onSuccess: (data, _variables, _onMutateResult, { client }) => {
    client.setQueryData(['todo', data.id], data)
  },
})

You can use the same client for invalidation:

onSuccess: async (_data, _variables, _onMutateResult, { client }) => {
  await client.invalidateQueries({ queryKey: ['todos'] })
}

So you do not need to pass the client into the options factory manually. If your installed version does not expose the fourth argument, it predates the mutation c…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@eprig
Comment options

Answer selected by eprig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants