TanStack Query Open-source server-state library for fetching, caching and synchronizing asynchronous data with configurable query lifecycles.
Overview TanStack Query, formerly React Query, is an open-source library for managing asynchronous server state in application interfaces. It tracks fetched data, loading and error states and cache lifecycles around application-supplied query functions. It can replace repeated fetching and synchronization code, but it does not supply the backend or make cached data automatically authoritative.[2] [5]
Contents Server state rather than every application state The library addresses data obtained asynchronously from another system, where the displayed copy can become stale independently of local UI actions. A QueryClient and provider coordinate the React integration, while query functions perform the actual work. This separates cache and request lifecycle handling from the backend transport. It should not be read as a replacement for authentication, a database or all local component state.[2]
Cache identity is part of correctness Query keys identify the data being fetched and act as dependencies. The guide says to include changing variables used by the query function, such as the selected record or filter. Object-property order is normalized in hashing, while array order matters. A generated interface that changes its request parameters without changing its key can therefore have the wrong cache identity even when the request function looks correct.[4]
Freshness and retention are different settings Cached data is stale by default, and stale queries may refetch on mounting, window focus or reconnection. staleTime changes that freshness behavior; inactive-cache retention is controlled separately by gcTime. The documented defaults include five-minute inactive retention and retry behavior. Infinity still permits manual invalidation, whereas static blocks invalidation-triggered refetches. These distinctions should be chosen for the resource rather than used as interchangeable ways to silence network activity.[3]
The query function defines failure A successful query must resolve data other than undefined; null can represent an intentionally empty result. To enter the error state, its function must throw or return a rejected promise. Native fetch does not throw simply because an HTTP response is unsuccessful, so the guide checks the status and throws explicitly. This boundary is useful when reviewing generated fetchers that otherwise parse an error response as successful data.[5]
Cancellation needs transport cooperation TanStack Query supplies an AbortSignal to the query function, but an unused query's in-flight promise is not cancelled by default merely because its component unmounts. Passing the signal to a supported transport allows actual cancellation and restores prior query state. The documentation excludes the Suspense query hooks from this cancellation behavior. A cancel button should therefore be checked against the chosen hook and transport, not inferred from the presence of a library method.[6]
Server rendering and data separation The SSR guide describes prefetching, serializing the cache and hydrating it on the client. It warns against a file-level shared QueryClient that would mix data across server requests and users. Request-scoped cache creation is consequently a privacy and correctness requirement in that setup, not just a performance preference. The article does not claim a particular application's hydration or user isolation has been tested.[7]
License and substitution TanStack Query is MIT-licensed. It can replace SWR for suitable React data-fetching and caching workflows, with an explicit migration of keys, request functions and refresh policy. No drop-in API compatibility, benchmark or tested application migration is claimed. Applications still need their own decisions about invalidation and backend success.[1] [2] [5] [8]
Also included: 5 capabilities and 0 substitute relationships. Check substitutes in section 1 and capabilities in section 3.