query
query copied to clipboard
Updates to staleTime via setQueryDefaults doesn't appear to take effect
Describe the bug
I have a login endpoint that on successful login returns the session expiry time. On successful login, I want to set the staletime
of the login query to the returned session expiry time as there is no point re-querying the login endpoint while the session is still active.
It appears that once the staletime
is set initially on mount, calling setQueryDefaults
with a staletime
doesn't change it.
Your minimal, reproducible example
https://codesandbox.io/s/react-query-change-staletime-nmw8cp?file=/src/index.tsx
Steps to reproduce
- You can see that when the
Issue
component mounts, the logs indicate that thestaletime
is set toInfinity
. - The login query returns and the logs indicate that the
staletime
is set to one second. - After one second the query remains fresh
Expected behavior
As a use I would have expected the staletime
to change to one second and the query becomes stale after one second.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
See codesandbox
react-query version
3.39.1
TypeScript version
No response
Additional context
No response
It appears that once the staletime is set initially on mount, calling setQueryDefaults with a staletime doesn't change it.
That is correct - the defaults are picked up when the query is created. You would need to use local state for that at the moment.