Skip to content

Query keeps retrying despite 'enabled' set to false #9138

@alekseyrozh

Description

@alekseyrozh

Describe the bug

This issue raised in the past - #1380 which is now marked as resolved, however I would want to highlight a usecase that doesn't look right.

function useApps(enabled: boolean) {
  const queryResult = useQuery({
    queryKey: ['apps'],
    queryFn: () => fetch(`/api/apps`),
    enabled: enabled,
  });
  return queryResult;
}

If the query starts, the first fetch fails, and then enabled turns to false due to some other reason, the fetch keeps being retried according to the retry policy. If in the end it still fails the queryResult will have an error status which will show up after all the retries are done. In my case with 3 retries ~15 seconds after enabled was set to false.

I presume adding enabled to query keys might help, but this feels wrong and also kind of contradicts the point of having an explicit "enabled" param.

queryKey: ['apps', { enabled }],

Your minimal, reproducible example

https://codesandbox.io/p/devbox/magical-rubin-ryf6wt?workspaceId=ws_GqRnfu5ueZhkd8SSdfMDQo

Steps to reproduce

  • Have a query which would fail
  • Have an enabled param to it
  • Set enabled to false while the query is being retried
  • See this not taking any effect

Expected behavior

When enabled becomes false, the query should not be retried and the state should be whatever it was before this query started, aligning with this doc

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Browser: Arc

Tanstack Query adapter

None

TanStack Query version

5.54.1

TypeScript version

5.5.4

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