T
TanStack3y ago
foreign-sapphire

Triggering a request before the previous request with the same key is completed

Say a useMutation may be triggered by multiple conditions -> lets consider it being called (almost) simultaneously. Before the previous mutation even finished. What happens when that is the case? Does React Query care about this? Does it wait untill the ongoing mutation request returned 200 or 500 etc.? Or does my backend have to make sure that multiple requests to this endpoint stay patient?
4 Replies
foreign-sapphire
foreign-sapphireOP3y ago
Or maybe I should control wether the mutation is about to run with the enabled boolean option.
foreign-sapphire
foreign-sapphire3y ago
You can also use the isLoading flag returned by useMutation() to know if a call is already in progress before calling mutate again. The useIsMutating() hook may also help: https://tanstack.com/query/v4/docs/react/reference/useIsMutating
useIsMutating | TanStack Query Docs
useIsMutating is an optional hook that returns the number of mutations that your application is fetching (useful for app-wide loading indicators). `tsx
xenial-black
xenial-black3y ago
No, TanStack Query isn't opinionated about this. It'll fire two mutations if you call mutate twice. This is something you can handle yourself wherever you feel is most appropriate
foreign-sapphire
foreign-sapphireOP3y ago
Thanks

Did you find this page helpful?