Replies: 3 comments 3 replies
-
I think there are a couple of concepts being mixed here, let's try to separate them please:
I've heard this request before, but I'm not really getting the concept. As far as I'm aware, query cancellation cancels the network request so that the browser does not need to download the rest of the data. This is optional in react-query, and possible by attaching a How will this work with mutations? I'm not finding anything on the internet what happens physically when a request is aborted. All examples are for get requests only. I know that the request method per se doesn't matter, but let's presume the following situation:
Maybe I'm just overthinking this, but I don't understand how one can physically abort a mutation that actually has a side-effect on the server ...
calling the
would it not? |
Beta Was this translation helpful? Give feedback.
-
Thank you for responding so quickly! As for cancelling a mutation: HTTP requests are currently performed over the TCP/IP protocol, which guarantees that a packet either arrives in its entirety or gets dropped. If a mutation with a large payload is executed (e.g. with an image weighing ~5MB), we could save some bandwidth by canceling the upload procedure early. The server doesn’t perform any business logic on dropped transfers. (The same thing applies to accidental disconnects.) Debouncing a mutation as you’ve suggested doesn’t work together with optimistic updates. In your example, React Query’s However, when a debounce happens (e.g. |
Beta Was this translation helpful? Give feedback.
-
Hey everyone, I needed to implement debouncing for the mutate in my code, and here’s the solution I came up with. If you also need to debounce the mutate function, you can use lodash’s debounce with both the trailing and leading options as shown below: import { useMutation, useQueryClient } from "@tanstack/react-query"
import { fetchClient } from "@/lib/fetch"
import debounce from "lodash.debounce"
export const useSaveMutation = () => {
const queryClient = useQueryClient()
const saveArticlesFn = debounce(async (data: any) => {
const response = await fetchClient({
url: `articles/${data.uuid}`,
method: "put" ,
body: data
})
return response?.data
}, 4000, { leading: true, trailing: true })
return useMutation({
mutationFn: saveArticlesFn,
onSuccess: async data => {
await queryClient.invalidateQueries({
queryKey: useArticleKeys.filtered(params)
})
},
onMutate: async (data: any) => {
console.log(data)
queryClient.setQueryData(
useArticleKeys.detail(data?.uuid),
(prev: any) => ({ ...prev, ...data })
)
},
onError: (error, variables) => {
console.error(error)
queryClient.setQueryData(
useArticleKeys.detail(slug as string),
prev => prev
)
}
})
} |
Beta Was this translation helpful? Give feedback.
-
Hello,
I’ve been using React Query for a while and one of the features I miss the most is mutation debouncing. After having a discussion over trpc/trpc#368, I realized there’s a way to implement a
useDebouncedMutation
hook in user-land, even though it’s a bit of a struggle to make:I would love to see a
delayMs
(ordebounceMs
) parameter added into the coreuseMutation
hook. I think it should also be possible to cancel mutations silently – without flooding the console – but unfortunately, the currentmutation.cancel
method doesn’t allow that. (CancelledError
, which is already being utilized by queries, can take asilent
argument, though.)Also, overriding
retry
wouldn’t be necessary with built-in support for handlingCancelledError
instances within mutations.In order to support more browsers,
AbortController
could be made optional (checked withtypeof AbortController !== "undefined"
), while keeping track of which mutations to execute, e.g. with a counter.Beta Was this translation helpful? Give feedback.
All reactions