tanstack-best-practices
Installation
SKILL.md
TanStack Query Best Practices for Hook Libraries
TanStack Query Core Concepts
Cache Model
TanStack Query is a key-value cache for async data. Two components using the same query key share one cache entry and one network request (deduplication). If Component B mounts while Component A's fetch is in-flight, B subscribes to A's request — no duplicate.
staleTime vs gcTime
These control the cache lifecycle:
staleTime(default: 0) — how long data is "fresh" after fetching. While fresh, TQ serves from cache without refetching. Once stale, TQ refetches in the background on mount/focus (stale-while-revalidate: user sees old data instantly, then re-renders with fresh data).gcTime(default: 5 min) — how long unused cache entries survive after all observing components unmount. After gcTime, the entry is garbage-collected.
Timeline: fetch -> fresh (staleTime) -> stale -> component unmounts -> gcTime expires -> entry deleted
If a component remounts within gcTime with stale data: instant cached data + background refetch. If a component remounts after gcTime: full loading state + fresh fetch.