TanStack Query v5 separates server state from client state. It defines a cache keyed by query keys, controls freshness with staleTime, manages garbage collection with gcTime, and coordinates background refetches. mastering the cache model eliminates the useEffect+fetch+loading-state boilerplate that plagues most React apps while giving you automatic background refresh, optimistic updates, and Suspense integration.
Optimistic mutation pattern with rollback on error.
User profile: staleTime: 1000 * 60 * 5 (5 min). Real-time data: staleTime: 0. Static reference data: staleTime: Infinity.
Include all variables that affect the fetch in the key: ["posts", { userId, status }]. TanStack Query refetches when the key changes.
Wrap in <Suspense> and the component only renders when data is ready. Eliminates isLoading/isError conditional rendering.
Sign in to share your feedback and join the discussion.