# TanStack Query

Open\-source server\-state library for fetching, caching and synchronizing asynchronous data with configurable query lifecycles\.

ID: Q933 · Revision: 981

Type: software · License: MIT[^license] · Status: active

Website: https://tanstack.com/query



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\.[^overview][^functions]

## 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\.[^overview]

## 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\.[^keys]

## 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\.[^defaults]

## 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\.[^functions]

## 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\.[^cancel]

## 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\.[^ssr]

## License and substitution

TanStack Query is MIT\-licensed\. It can replace [SWR](/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\.[^license][^overview][^swr][^functions]

## Capabilities and substitutions

### Server\-state caching — supported

Tracks asynchronous results by query key and exposes loading/error/data states\.[^overview][^keys]

Use case: Fetching and caching server data in application interfaces · Platforms: Web applications; React adapter documentation inspected · Region: Version, build and integration requirements apply

### Controlled cache freshness — supported

staleTime controls freshness while gcTime concerns inactive retention; static is stricter than Infinity\.[^defaults]

Use case: Fetching and caching server data in application interfaces · Platforms: Web applications; React adapter documentation inspected · Region: Version, build and integration requirements apply

### Automatic HTTP error recognition for any fetcher — limited

Query functions must throw or reject; native fetch status failures require explicit handling\.[^functions]

Use case: Fetching and caching server data in application interfaces · Platforms: Web applications; React adapter documentation inspected · Region: Version, build and integration requirements apply

### Network cancellation — limited

Consume the supplied AbortSignal to cancel supported requests; the guide excludes Suspense query hooks\.[^cancel]

Use case: Fetching and caching server data in application interfaces · Platforms: Web applications; React adapter documentation inspected · Region: Version, build and integration requirements apply

### SSR hydration — supported

Prefetching and hydration are documented, with per\-request cache isolation required\.[^ssr]

Use case: Fetching and caching server data in application interfaces · Platforms: Web applications; React adapter documentation inspected · Region: Version, build and integration requirements apply

### [Substitute for SWR](/swr) — limited

Can 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\.[^overview][^defaults][^functions][^swr]

Use case: Fetching and caching server data in application interfaces · Platforms: Web applications; React adapter documentation inspected · Region: Version, build and integration requirements apply

## References

[^license]: [TanStack Query license](<https://github.com/TanStack/query/blob/main/LICENSE>) — MIT terms (vendor; retrieved 2026-10-07T23:14:37Z)

[^overview]: [TanStack Query overview](<https://tanstack.com/query/latest/docs/framework/react/overview>) — Server state and basic React query workflow (vendor; retrieved 2026-10-07T23:13:58Z)

[^defaults]: [TanStack Query defaults](<https://tanstack.com/query/latest/docs/framework/react/guides/important-defaults>) — Staleness, refetching, static/Infinity and garbage collection (vendor; retrieved 2026-10-07T23:14:06Z)

[^keys]: [TanStack query keys](<https://tanstack.com/query/latest/docs/framework/react/guides/query-keys>) — Changing query dependencies belong in keys (vendor; retrieved 2026-10-07T23:15:02Z)

[^functions]: [TanStack query functions](<https://tanstack.com/query/latest/docs/framework/react/guides/query-functions>) — Rejected promises, undefined and fetch status handling (vendor; retrieved 2026-10-07T23:15:27Z)

[^cancel]: [TanStack query cancellation](<https://tanstack.com/query/latest/docs/framework/react/guides/query-cancellation>) — AbortSignal consumption and Suspense limitations (vendor; retrieved 2026-10-07T23:15:11Z)

[^ssr]: [TanStack server rendering](<https://tanstack.com/query/latest/docs/framework/react/guides/ssr>) — Request\-scoped QueryClient and hydration (vendor; retrieved 2026-10-07T23:14:37Z)

[^swr]: [SWR getting started](<https://swr.vercel.app/docs/getting-started>) — Key/fetcher and React data\-fetching hook (vendor; retrieved 2026-10-07T23:13:58Z)

Source revision: https://substitutefor.net/entities/Q933?revision=981
