#4448·trpc

docs: useQuery abortOnUnmount has more effects than the name and documentation suggest

Author: keanemindCreated May 29, 2023Updated Sep 20, 2026
Labels📚 documentation / examples👋 free for all

Area of Improvement

I had a computationally expensive (for the server) useQuery whose input was tied to what the user typed. I wanted my fetch requests to be cancelled as soon as possible if the input was changed, to keep the server from doing unnecessary work and also avoid caching a bunch of responses that weren't going to be used. From reading the React Query docs (note the Default Behavior section, which I've quoted below), I knew I wanted tRPC to consume the signal from the query function; consuming the signal would cause react-query to cancel and throw away any in-flight requests for unused queries immediately. It took me a while, but I eventually found out about tRPC's abortOnUnmount, and correctly suspected that setting that option would cause tRPC to consume the signal as I wanted.

So the issue is that abortOnUnmount doesn't just cause the query to abort on unmount. It causes tRPC to consume the signal from react-query, which has the additional effect of causing react-query to cancel the unfinished request for your previous query key if you change your useQuery to have a new query key mid-request (and this can happen whenever you change your tRPC input). The tRPC documentation on the website and in the JSDoc doesn't explicitly state that setting the flag will cause tRPC to consume the AbortSignal provided by react-query.

From the react-query docs, emphasis mine:

By default, queries that unmount or become unused before their promises are resolved are not cancelled. This means that after the promise has resolved, the resulting data will be available in the cache. This is helpful if you've started receiving a query, but then unmount the component before it finishes. If you mount the component again and the query has not been garbage collected yet, data will be available.

However, if you consume the AbortSignal, the Promise will be cancelled (e.g. aborting the fetch) and therefore, also the Query must be cancelled. Cancelling the query will result in its state being reverted to its previous state.

Link to related docs

https://trpc.io/docs/reactjs/aborting-procedure-calls

Additional information

No response

‍‍ Contributing

  • ‍♂️ Yes, I'd be down to file a PR implementing the suggested changes!