Fetching and caching server data in application interfaces limited
TanStack Query → SWR
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.
Platforms: Web applications; React adapter documentation inspected · Region: Version, build and integration requirements apply
Fetching, refreshing and mutating server-backed data in React interfaces limited
SWR → TanStack Query
Can replace TanStack Query for React data fetching, caching and supported mutation flows after rewriting hooks and policies. Provider boundaries, key dependencies and refetch behavior must be explicitly migrated.
SWR is an open-source React library for data fetching and caching. A component supplies a key and a fetcher, while SWR coordinates cached data and refresh behavior. Mutation helpers support updating the interface around remote writes. It supplies client data-state machinery, not a backend or proof that an optimistic UI change has been saved.[2][6][7]
Separating fetching from cache behavior
SWR accepts application-defined fetcher logic, with the introductory guide showing a native fetch wrapper and alternatives for other request clients. This lets the same hook pattern work with different APIs. The application still owns transport details and how a response becomes useful data; the library does not discover an API's business rules merely from a URL.[2]
Keys must describe the changing request
The arguments guide warns that a request using changing authorization context while retaining a fixed key can return the wrong cached data. The key must capture the dependencies that distinguish results. It also documents changed array-argument behavior between older versions and version 2 onward. Existing fetcher signatures and context-sensitive keys should therefore be reviewed together when migrating generated or older code.[3]
Refresh policy and request volume
SWR revalidates on window focus and network reconnection by default. Timed refreshing is optional, with hidden and offline refresh controls disabled by default. An immutable-resource helper disables automatic revalidation for data that really should not change. As an assessment, these settings should match the resource and service limits; disabling refresh for changing account data is a behavior change, not simply a harmless optimization.[4]
Stale data can coexist with an error
A fetcher rejection produces an error state. The error-handling guide explicitly checks non-success HTTP statuses and throws rather than assuming valid JSON means success. It also notes that data and error can be present simultaneously, allowing the UI to retain an existing result while reporting a failed refresh. Retry behavior is configurable. A generated component needs to decide how that stale-but-visible state should look.[5]
Custom caches change mutation scope
The default shared cache can be replaced with a Map-like provider under SWRConfig. Hooks inside that boundary use that provider, and the globally imported mutate does not operate on them in the same way. The documentation points to useSWRConfig for the relevant scoped mutator and discourages direct cache writes. Remounting a provider can also recreate its cache, so its placement affects more than code organization.[6]
Optimistic updates are provisional
Mutation helpers can display optimistic data, populate the cache from a response and restore prior local data when a remote operation fails. That rollback changes the local cache; it does not establish reversal of a completed server operation. The guide also warns that calling a global mutator with only a key does not revalidate or update the cache unless a matching hook is mounted. These are important boundaries for generated save buttons and background update flows.[7]
License and substitution
SWR uses MIT terms. It can replace TanStack Query for suitable React server-data workflows if the application migrates its keys, fetchers, cache boundaries and refresh policy. This comparison does not establish API compatibility or replace the backend's persistence and authorization rules. No installation, performance test or application migration was performed.[1][2][6][8]
Capabilities & limitations
Fetcher-independent data access
supported
Application supplies fetcher logic; REST, GraphQL or other clients can be integrated.[2]
Fetching, refreshing and mutating server-backed data in React interfaces · React applications · Version, build and integration requirements apply