T
TanStack4y ago
flat-fuchsia

mutation callback not running if the component unmounts

Why is this? Is this a technically limitation or a design choice? What is the rationale for this?
4 Replies
sunny-green
sunny-green4y ago
Mutations | TanStack Query Docs
Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects. For this purpose, React Query exports a useMutation hook. Here's an example of a mutation that adds a new todo to the server:
sunny-green
sunny-green4y ago
You might find that you want to trigger additional callbacks beyond the ones defined on useMutation when calling mutate. This can be used to trigger component-specific side effects. To do that, you can provide any of the same callback options to the mutate function after your mutation variable. Supported overrides include: onSuccess, onError and onSettled. Please keep in mind that those additional callbacks won't run if your component unmounts before the mutation finishes.
You might find that you want to trigger additional callbacks beyond the ones defined on useMutation when calling mutate. This can be used to trigger component-specific side effects. To do that, you can provide any of the same callback options to the mutate function after your mutation variable. Supported overrides include: onSuccess, onError and onSettled. Please keep in mind that those additional callbacks won't run if your component unmounts before the mutation finishes.
sunny-green
sunny-green4y ago
Mastering Mutations in React Query
Learn all about the concept of performing side effects on the server with React Query.
flat-fuchsia
flat-fuchsiaOP4y ago
Ah yeah okay I can see that Haven't read the docs in a while we're still on v3 and I basically have most things memorized at this point

Did you find this page helpful?