Skip to content

A call to fetchQuery is cancelled when React useQuery is unmounted #9798

@bartlangelaan

Description

@bartlangelaan

Describe the bug

While integrating Tanstack Query with another library, I found queryClient.fetchQuery() to fail the first time during development. The promise was cancelled when some component was using the same query with useQuery - only during development, when using StrictMode.

But it can also happen in production. If an useQuery is unmounted, it cancels the query if it is the last one observing the query. Even if some other code is running queryClient.fetchQuery(). I would expect Tanstack Query to know that fetchQuery is running and not cancel the query.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/tanstack-query-issue-9798-fetchquery-cancelled-gkt869

Steps to reproduce

  1. Open the codesandbox
  2. The useQueryResult will succesfully show the amount of stargazers, but the fetchQueryResult will contain a CancelledError.

Expected behavior

As a user, I expected the fetch not to be cancelled because fetchQuery was called. I would expect that unrendering a useQuery call would not in any way influence a call to fetchQuery anywhere in the application.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Linux, Mac
  • Browser: Edge, Chrome 141.0.7390.76

Tanstack Query adapter

react-query

TanStack Query version

v5.90.5

TypeScript version

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions