Replies: 5 comments 11 replies
-
I think preventing the call of a function (in this case: there is also a global |
Beta Was this translation helpful? Give feedback.
-
An example solution is that if you have a button with something like const isMutating = Boolean(useIsMutating(['mutationKey']));
// use whatever library you want for your throttle
const throttledSubmit = useThrottle(onSubmit, 1000);
<Button onClick={throttledSubmit} disabled={isMutating}>Submit</Button> For me, I was using Formik and the |
Beta Was this translation helpful? Give feedback.
-
Knowing that you sometimes have to use a hack like |
Beta Was this translation helpful? Give feedback.
-
for those who are looking for the answer: I used the below way to achieve global mutation deduplication: inside onMutate(variables) {
// Variable to store the mutation key
let mutationKey = "";
// Object containing the maximum number of allowed mutations for each query key.
// If a query key is not present, it defaults to 1 (only one mutation at a time)
const maxConcurrentMutations = {
["foo"]: 1,
};
// Get the mutation count for the current mutation
const currentMutationCount = qc.isMutating({
// Find all mutations that match the current mutation
predicate(mutation) {
mutationKey = JSON.stringify(mutation.options.mutationKey);
// because the variables are objects, we need to use a deep comparison
return isEqual(mutation.state.variables, variables);
},
// make a strict comparison
exact: true,
// only consider mutations that are currently in flight
fetching: true,
});
// If the mutation count is greater than its allowed count, it means that there is already a mutation
// in flight for the same input. In that case, we reject the mutation
if (
currentMutationCount > (maxConcurrentMutations[mutationKey] || 1)
) {
return Promise.reject();
}
}, |
Beta Was this translation helpful? Give feedback.
-
The best approach I've found so far is to utilize mutation states. You can check for isPending & isSuccess before executing mutation. And reset mutation on success. I tried utilizing debounce/throttle of lodash, but it was not 100% fool proof. Mutation states are more consistent & easier to reason with. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It would be great to have an option to prevent multiple multations from being triggered. So that if a user fires a mutation multiple times, none of those does not go through if there is already one in progress.
I can use
isLoading
for it but sometimes I have a mutation that can be triggered for multiple places and checking the loading status everywhere is not ideal.Something like this, maybe?
Beta Was this translation helpful? Give feedback.
All reactions