refetchOnMount: false is not respected when the query failed #10018
Describe the bug#
Queries that have an error status seem to refetch on mount even when refetchOnMount is false.
Your minimal, reproducible example#
https://github.com/EdiOanceaV2/tanstack-query-refetch-bug
Steps to reproduce#
Run the example, check the console log happening every second.
Expected behavior#
I believe the query should only run once, not continuously.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
- Mac OS
- Chrome
- latest
Tanstack Query adapter#
react-query
TanStack Query version#
v5.90.16
TypeScript version#
5.8.3
Additional context#
It broke in this specific version.
TkDodo added labelsworks as designed
Hi @TkDodo, thanks for the quick response! So the reason for this is because retries and refetches are inherently different? Would you say simply setting
retryOnMounttofalseon the query client itself would make it work as before?a rety is not a refetch, but
retryOnMountdoesn’t really have anything to do with retries - it’s awkwardly named.The docs are correct about what it does:
If set to `false`, the query will not be retried on mount if it contains an error. Defaults to `true`.here, retried means: the query is in an error state, has no stale data that can be shown, so when it mounts, we trigger a fetch. That puts it into
status: pendingbecause it has no data - a “hard loading state”.usually, a retry is a process where when the
queryFnfailed, we run it again after some time. This is an internal, mostly opaque process because the query will only go intoerrorstate after all retries have been exhausted.Would you say simply setting retryOnMount to false on the query client itself would make it work as before?
not sure what “as before” means here but if you set this to false and your query is an error state, it will not refetch when you try to use it again, which I don’t think is what you want (I don’t know what you want ...). Error’d queries are always considered stale so it will refetch eventually when a trigger occurs, so you’d just be delaying the problem.
your reproduction says:
this will call again and again every second which means isPending goes from true -> false -> true
which happens because you create a loop by unmounting the child when your query is pending:
- parent triggers fetch
- fetch fails
- now you render child
- this triggers fetch because reasons mentioned above
- this now unmounts child because parent does this:
if (queryThatFails.isPending) { return null } return <ChildComponent />so yeah, “don’t do this” is my advice. This also isn’t a realistic scenario. It comes up once every blue moon and an issue is filed, but it goes away if you implement proper error handling, either via error boundaries (
throwOnError) or by handling errors in the parent component:if (queryThatFails.isPending) { return null } if (queyThatFails.isError) { return “error” } return <ChildComponent />TkDodo closed this
By as before I meant strictly that this only started becoming an issue in
.16while'.15worked perfectly fine. I thought it was a breaking change in a patch version but I'm assuming the fact it worked previously was in fact a bug.I'm relying on the error state to know that X didn't happen yet so in a sense I'm not using it as it was intended. Thank you for the clarification.
Sign in to join this conversation.
that’s intentional because errors are always treated as stale if we don’t have any data to show. If you want errors to persist, best thing would be to show an ErrorBoundary, or set
retryOnMounttofalse.