query icon indicating copy to clipboard operation
query copied to clipboard

Updates to staleTime via setQueryDefaults doesn't appear to take effect

Open michael-wolfenden opened this issue 2 years ago • 1 comments

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

  1. You can see that when the Issue component mounts, the logs indicate that the staletime is set to Infinity.
  2. The login query returns and the logs indicate that the staletime is set to one second.
  3. 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

michael-wolfenden avatar Jun 20 '22 11:06 michael-wolfenden

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.

TkDodo avatar Jun 20 '22 11:06 TkDodo