EasyState Management📖 Theory Question

What is React Query's staleTime vs gcTime (cacheTime) and how do they interact?

💡

Hint

staleTime = how long data is considered fresh; gcTime = how long inactive cache entries are kept before garbage collection

Full Answer

React Query uses two separate timers for different parts of the cache lifecycle.

staleTime — how long a query's data is considered "fresh" after it was last fetched. During this window, React Query won't refetch even if the component remounts or the user refocuses the window.

  • Default: 0 — data is immediately stale, so React Query refetches on every mount/focus.
  • Set to Infinity for data that never changes (static config, user roles).
  • Set to 5 * 60 * 1000 (5 min) for data that changes infrequently.

gcTime (formerly cacheTime) — how long an inactive (no subscribers) query result stays in the cache before being garbage-collected.

  • Default: 5 * 60 * 1000 (5 min).
  • While in cache, navigating back to a page shows the cached data instantly (even if stale) while refetching in the background.
useQuery({
  queryKey: ['user', id],
  queryFn: fetchUser,
  staleTime: 60_000,   // fresh for 1 min — no refetch on focus
  gcTime: 300_000,     // keep in cache 5 min after last subscriber
});

Key interaction: staleTime ≤ gcTime is the natural order. staleTime controls when to refetch; gcTime controls when to forget.

More State Management Questions

EasyWhat is the difference between server state and client state?EasyWhen would you choose Zustand over Redux Toolkit?EasyWhat is optimistic updating and how do you implement it with React Query?EasyWhen should you use URL state instead of React state?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint