T
TanStack•10mo ago
fascinating-indigo

How to disable retries when network request is cancelled?

I have a situation where I need to imperatively call queryClient.fetchQuery() to fetch data. Sometimes the network request from this query get cancelled by Chrome where the network status is "(canceled)" (see image). Is there a way to disable retries when this happens? Thank you!
No description
3 Replies
harsh-harlequin
harsh-harlequin•10mo ago
How about a retry function? This is pseudo code obviously.
useQuery({
queryFn: () => {
if (response.status === 'canceled') {
throw new Error('canceled');
}
},
retry: (failureCount, error) => {
if (error.message === "canceled") {
return false;
}

return failureCount < 3;
},
}
});
useQuery({
queryFn: () => {
if (response.status === 'canceled') {
throw new Error('canceled');
}
},
retry: (failureCount, error) => {
if (error.message === "canceled") {
return false;
}

return failureCount < 3;
},
}
});
sensitive-blue
sensitive-blue•10mo ago
yeah this is how you'd do it. If you know the error is a "canceled" error (not sure if we get that from fetch), then you can implement a retry function that checks for that. That's exactly why retry can be a function 🙂
fascinating-indigo
fascinating-indigoOP•10mo ago
Thanks all. The above is what I tried to do at first. For some reason, the retry hook isn't being called when this happens. The client doing the fetching is a custom wrapper around Axios. I need to dig in more to see why the error thrown by it is not triggering retry.

Did you find this page helpful?