software · Q933
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]
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]
Capabilities & limitations
Server-state caching
supportedTracks asynchronous results by query key and exposes loading/error/data states.[2][4]
Fetching and caching server data in application interfaces · Web applications; React adapter documentation inspected · Version, build and integration requirements apply
2 sources · check evidence
Server state and basic React query workflow
vendor · checked 2026-10-07Changing query dependencies belong in keys
vendor · checked 2026-10-07Controlled cache freshness
supportedstaleTime controls freshness while gcTime concerns inactive retention; static is stricter than Infinity.[3]
Fetching and caching server data in application interfaces · Web applications; React adapter documentation inspected · Version, build and integration requirements apply
1 source · check evidence
Staleness, refetching, static/Infinity and garbage collection
vendor · checked 2026-10-07Automatic HTTP error recognition for any fetcher
limitedQuery functions must throw or reject; native fetch status failures require explicit handling.[5]
Fetching and caching server data in application interfaces · Web applications; React adapter documentation inspected · Version, build and integration requirements apply
1 source · check evidence
Rejected promises, undefined and fetch status handling
vendor · checked 2026-10-07Network cancellation
limitedConsume the supplied AbortSignal to cancel supported requests; the guide excludes Suspense query hooks.[6]
Fetching and caching server data in application interfaces · Web applications; React adapter documentation inspected · Version, build and integration requirements apply
1 source · check evidence
AbortSignal consumption and Suspense limitations
vendor · checked 2026-10-07SSR hydration
supportedPrefetching and hydration are documented, with per-request cache isolation required.[7]
Fetching and caching server data in application interfaces · Web applications; React adapter documentation inspected · Version, build and integration requirements apply
1 source · check evidence
Request-scoped QueryClient and hydration
vendor · checked 2026-10-07Can substitute for
These are products or services this entry can replace. Check the task and limitations for each comparison.
SWR for Fetching and caching server data in application interfaces
limitedCan replace SWR for React server-data fetching and caching after adapting keys, query functions and refresh policies. Cache defaults, mutation integration and cancellation are not interchangeable APIs.[2][3][5][8]
Web applications; React adapter documentation inspected · Version, build and integration requirements apply
References
- TanStack Query license
MIT terms
vendor · retrieved 2026-10-07 - TanStack Query overview
Server state and basic React query workflow
vendor · retrieved 2026-10-07 - TanStack Query defaults
Staleness, refetching, static/Infinity and garbage collection
vendor · retrieved 2026-10-07 - TanStack query keys
Changing query dependencies belong in keys
vendor · retrieved 2026-10-07 - TanStack query functions
Rejected promises, undefined and fetch status handling
vendor · retrieved 2026-10-07 - TanStack query cancellation
AbortSignal consumption and Suspense limitations
vendor · retrieved 2026-10-07 - TanStack server rendering
Request-scoped QueryClient and hydration
vendor · retrieved 2026-10-07 - SWR getting started
Key/fetcher and React data-fetching hook
vendor · retrieved 2026-10-07
Discussion & disputes
Ask about unclear claims or explain a concern with supporting sources. If you have a specific fix, suggest a correction.
No discussion yet.
Revision history
Open a revision to read an earlier version of this article.
| Revision | Timestamp | Edit summary |
|---|---|---|
| 981 | 2026-10-13 15:59:07 | /* wbeditentity-create-item:0| */ sf:e5461550-bab5-49a7-9439-fc7a17bc450a:v1 | Adds TanStack Query with cache-key dependencies, refetch/garbage-collection distinction, error and cancellation contracts and request-scoped SSR caches. | attribution: /proposals/e5461550-bab5-49a7-9439-fc7a17bc450a |