Web / TanStack Interview Questions
What is the difference between staleTime and gcTime in TanStack Query?
These are the most commonly confused options — they govern completely different lifecycle phases.
| Option | Controls | Default |
|---|---|---|
| staleTime | How long cached data is 'fresh' — no background refetch while fresh | 0 ms (immediately stale) |
| gcTime | How long an unused cache entry (no subscribers) stays in memory before GC | 5 minutes |
const { data } = useQuery({ queryKey: ["config"], queryFn: fetchConfig, staleTime: 1000*60*10, // fresh 10 min â no background refetch gcTime: 1000*60*30, // kept 30 min after last subscriber leaves }) // t=0: fetched. FRESH. // t=8min: remount. Still FRESH â cache hit, no network call. // t=11min: remount. STALE â serves cache, refetches in background. // t=35min: unmounted 30 min â cache REMOVED by GC.
Tip: staleTime:Infinity means data never goes stale automatically — only explicit invalidateQueries() refreshes it. Ideal for app config or country lists.
More Related questions...