useIsMutating return wrong count of mutating calls #3332
Send to my agentDescribe the bug#
useIsMutating return wrong count of pending mutation when a failure occur
Your minimal, reproducible example#
https://codesandbox.io/s/great-zhukovsky-vovpcg?file=/src/useCustomMutations.js
Steps to reproduce#
1 - Have custom useMutation function with callbacks#
export const useCallBackMutation = () => {
return useMutation(
async () => {
await new Promise((_, rej) => setTimeout(() => rej(true), 2000));
return true;
},
{
mutationKey: CALL_BACK_MUTATION_KEY,
onError: async (error) => {
return Promise.reject(error);
},
onSuccess: async (data) => {
return data;
}
}
);
};
2 - Listen for useIsMutation value#
const value = useIsMutations()
3 - check number is incorrect when useMutation fails#
1 - # instead of 0
Expected behavior#
useIsMutations() should return 0, when there are no mutations pending
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
- OSX
- Chrome 98
react-query version#
3.34
TypeScript version#
No response
Additional context#
No response
hmm, the callbacks don't need to reject or resolve the promise again? If the mutationFn rejects, the mutation goes to error state. Just remove the callbacks:
return useMutation(async () => { await new Promise((_, rej) => setTimeout(() => rej(true), 2000)); return true; });the returned promise from
onErroris awaited by react-query so that you canreturn queryClient.invalidateQueries('my-query')or so after a mutation.From whatI can see, if the promises returned from the onError callback reject, we will never go into error state:
if
onErrororonSettledrejects,thedispatch({ type: error})` is not reached:if the callback for
onSuccessfails, we will go into the catch and thus in the error state.funny thing: If the mutationFn succeeds, but then the
onSettledcallback rejects, we will call theonErrorcallback and then again theonSettledcallback.@TkDodo thank u i see now,
i used the callbacks to perform side effects and I wanted the caller to get the error too, but i see no need to reject onError because is already propagating thanks for the response!
@TkDodo Although the count seems wrong to me anyway, and could lead to a never ending loading state thanks again :)
TkDodo added labelsbug
@FeliceGeracitano I think I have a fix for v4, we can just go to
errorstate if one of the callbacks errors.One question is: if the mutationFn throws an error, and then the
onSettledcallback throws another, but different error, which one would we expect to show up in the error state? The original one or the one fromonSettled🤔TkDodo added labelsv4
TkDodo closed this
:tada: This issue has been resolved in version 4.0.0-alpha.17 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
tannerlinsley added labelsreleased on @alpha
@TkDodo
That's a good question, i think depends on how
onError,onSettled&onSuccessare meant to be used.If they are just 'hooks' to events, the original error should be the one propagated, those call back should just run side-effect probably
yes, I also went with the initial error 👍
:tada: This issue has been resolved in version 4.0.0-beta.1 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
tannerlinsley added labelsreleased on @beta
Sign in to join this conversation.
As you can see from the reproducible example https://codesandbox.io/s/great-zhukovsky-vovpcg?file=/src/useCustomMutations.js
the workaround is to use the
try/catch approach