-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Query keeps retrying despite 'enabled' set to false #9138
Description
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