Docs: special behaviour of `initialDataUpdatedAt=0` is not documented #7003
Send to my agentDescribe the bug#
It seems useQuery works differently from what is written in the docs when initialDataUpdatedAt=0:
This option allows the staleTime to be used for its original purpose, determining how fresh the data needs to be, while also allowing the data to be refetched on mount if the initialData is older than the staleTime
So if I have the following config, useQuery shouldn't refetch data on mount:
const { data } = useQuery({
...
initialData: [],
initialDataUpdatedAt: 0, // it shouldn't refetch data as Date.now() - 0 < Infinity, but it does refetch in reality
staleTime: Infinity,
...
});
I guess this is by design as I found few issues/discussions where initialDataUpdatedAt: 0 was mentioned (for example, this and this).
However, there is no information about it on https://tanstack.com/query/v5/docs/framework/react/reference/useQuery and https://tanstack.com/query/v5/docs/framework/react/guides/initial-query-data.
Your minimal, reproducible example#
https://stackblitz.com/edit/github-krgozk
Steps to reproduce#
- Go to https://stackblitz.com/edit/github-krgozk
- Open Network tab in Dev Tools
- Find a request to https://api.github.com/repos/tannerlinsley/react-query (it shouldn't be made according to docs, as
initialDatais not older thanstaleTime)
Expected behavior#
Case with initialDataUpdatedAt=0 is documented on https://tanstack.com/query/v5/docs/framework/react/reference/useQuery and https://tanstack.com/query/v5/docs/framework/react/guides/initial-query-data.
How often does this bug happen?#
Every time
Screenshots or Videos#
Platform#
Chrome
Tanstack Query adapter#
None
TanStack Query version#
v5
TypeScript version#
No response
Additional context#
No response
okay, it's late. I think we shouldn't check for
dataUpdatedAthere at all. This check likely means: we're always stale if there is no data yet.!dataUpatedAtkind of reflects that, but not really. The check should likely be made againstthis.state.data !== undefinedlike we do here:TkDodo closed this
@TkDodo Ok, makes sense, thank you
Sign in to join this conversation.
it seems to be this condition specifically because zero is falsy in js 🙄
https://github.com/TanStack/query/blob/b32ad24f5d2ac79eb8dee32047195e2bb9267b2b/packages/query-core/src/query.ts#L253
It works if you set it to
-1for example.I would consider this a bug rather than a missing documentation. There a couple of those checks that we likely should change towards
> 0instead.