T
TanStack3y ago
mute-gold

Cancel useInfiniteQuery pending API's

Hi, I have a useInfiniteQuery search API that takes a a user entered search param and returns some results. I want to cancel any ongoing API calls if the user enters a new search param value. Any ideas on how to do this?
3 Replies
extended-salmon
extended-salmon3y ago
if search param is part of the query key, query cancellation via the provided AbortSignal should work out of the box: https://tanstack.com/query/v5/docs/react/guides/query-cancellation
Query Cancellation | TanStack Query Docs
TanStack Query provides each query function with an AbortSignal instance. When a query becomes out-of-date or inactive, this signal will become aborted. This means that all queries are cancellable, and you can respond to the cancellation inside your query function if desired. The best part about this is that it allows you to continue to use nor...
mute-gold
mute-goldOP3y ago
Thanks for the reply 🙏🏻 Yes, its part of the query key and I provided the signal to the queryFn as instructed in the docs.
extended-salmon
extended-salmon3y ago
Ok so it works?

Did you find this page helpful?