Skip to content

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

Open

Description

@keanemind

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!

Activity

  1. added
    📚 documentation / examples
    on May 29, 2023
  2. added
    👋 free for all
    on May 30, 2023
  3. TkDodo commented on Nov 6, 2023

    @TkDodo
    Contributor

    I think this more than just a documentation change - abortOnUnmount is wrongly named and we should consider renaming it for v11. It not only aborts on unmount or on QueryKey change - it will also abort when queryClient.cancelQueries() (or utils.someRoute.cancel() in trpc) is invoked.

    Two examples where this was discussed / misunderstood:

    renaming the option to abort: boolean and updating the documentation accordingly should be enough to clear up the confusion.

  4. keanemind commented on Nov 6, 2023

    @keanemind
    Author

    I concur. Ideally the field name is also changed, in addition to documenting the behavior.

  5. ArjunCodess commented on Sep 20, 2026

    @ArjunCodess

    hi, is this still wanted? the issue is from 2023 and on current main it still is not there: the docs still say abortOnUnmount only aborts on unmount, and the name is unchanged on v11.19.x. tkdodo said it also aborts on cancelQueries and should be renamed in v11. should i send a docs pr, a rename, or both?

  6. KATT commented on Nov 2, 2026

    @KATT
    Member

    🤖 In v12 there's no flag to misname: queries always pass TanStack's signal, which is v11's abortOnUnmount: true, so a query nothing observes any more (the component unmounted, or the input changed), or one hit by queryClient.cancelQueries(), aborts the call on both ends and reverts to its previous state instead of caching a late result. The docs say so: https://v12.trpc.io/v12/docs/integrations/tanstack-query#cancellation. For v11, @ArjunCodess, a docs-only PR saying the same about abortOnUnmount is welcome, without a rename. v12 isn't released yet and there's no ETA.

  7. added
    next-major/definite
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-major/definite👋 free for all📚 documentation / examples

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions