-
In the options for useMutation onSuccess I see the following description:
In the options for mutate onSuccess I see this description
I was wondering if onSuccess would only fire after the latest call to useMutation regardless of if I put the onSuccess in useMutation, mutate, or in the then of mutateAsync? I tested it out and it fires after every response from the server if onSuccess is in useMutation or if mutateAsync is used. The onSuccess in mutate is only fired after the latest call. Is this just a design limitation? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
This is on purpose. the callbacks on You can read more about this here: https://tkdodo.eu/blog/mastering-mutations-in-react-query#some-callbacks-might-not-fire |
Beta Was this translation helpful? Give feedback.
-
I have a variable amount of buttons that all trigger the same mutate. Each one takes around 5 seconds to complete. How can I do this? |
Beta Was this translation helpful? Give feedback.
This is on purpose. the callbacks on
.mutate
are observer level callbacks, and if you call mutate twice, the first observer will unsubscribe, which means you'll only get data / error for the second call (there is only one mutation after all), and the callbacks will only be invoked for the last one. However, the callbacks onuseMutation
are cache-level callbacks, so they are always called, even if there is no observer.You can read more about this here: https://tkdodo.eu/blog/mastering-mutations-in-react-query#some-callbacks-might-not-fire