# SWR

MIT\-licensed React data\-fetching library with shared caching, revalidation and mutation helpers\.

ID: Q901 · Revision: 949

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

Website: https://swr.vercel.app



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\.[^start][^cache][^mutation]

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

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

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

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

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

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

## License and substitution

SWR uses MIT terms\. It can replace [TanStack Query](/tanstackquery) 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\.[^license][^start][^query][^cache]

## Capabilities and substitutions

### Fetcher\-independent data access — supported

Application supplies fetcher logic; REST, GraphQL or other clients can be integrated\.[^start]

Use case: Fetching, refreshing and mutating server\-backed data in React interfaces · Platforms: React applications · Region: Version, build and integration requirements apply

### Automatic refresh policies — supported

Focus and network reconnect revalidate by default; intervals and immutable behavior are configurable\.[^revalidation]

Use case: Fetching, refreshing and mutating server\-backed data in React interfaces · Platforms: React applications · Region: Version, build and integration requirements apply

### Custom cache providers — supported

Map\-like providers scope cache behavior to an SWRConfig boundary\.[^cache]

Use case: Fetching, refreshing and mutating server\-backed data in React interfaces · Platforms: React applications · Region: Version, build and integration requirements apply

### Optimistic UI and local rollback — supported

Mutation helpers can show optimistic data and restore prior local cache on failure; this is not a server transaction rollback\.[^mutation]

Use case: Fetching, refreshing and mutating server\-backed data in React interfaces · Platforms: React applications · Region: Version, build and integration requirements apply

### Unsuccessful HTTP response detection — limited

The fetcher must turn unsuccessful status codes into errors; parsed JSON alone does not establish success\.[^errors]

Use case: Fetching, refreshing and mutating server\-backed data in React interfaces · Platforms: React applications · Region: Version, build and integration requirements apply

### [Substitute for TanStack Query](/tanstackquery) — limited

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\.[^start][^cache][^mutation][^query]

Use case: Fetching, refreshing and mutating server\-backed data in React interfaces · Platforms: React applications · Region: Version, build and integration requirements apply

## References

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

[^start]: [SWR getting started](<https://swr.vercel.app/docs/getting-started>) — React key/fetcher setup (vendor; retrieved 2026-10-07T23:13:58Z)

[^arguments]: [SWR arguments](<https://swr.vercel.app/docs/arguments>) — Entire key identity and changed fetcher context (vendor; retrieved 2026-10-07T23:15:20Z)

[^revalidation]: [SWR automatic revalidation](<https://swr.vercel.app/docs/revalidation>) — Focus/reconnect defaults, intervals and immutable resources (vendor; retrieved 2026-10-07T23:15:02Z)

[^errors]: [SWR error handling](<https://swr.vercel.app/docs/error-handling>) — Fetcher rejection, HTTP status checks and data/error coexistence (vendor; retrieved 2026-10-07T23:14:53Z)

[^cache]: [SWR cache providers](<https://swr.vercel.app/docs/advanced/cache>) — Provider boundaries and global mutate limitations (vendor; retrieved 2026-10-07T23:14:53Z)

[^mutation]: [SWR mutation and revalidation](<https://swr.vercel.app/docs/mutation>) — Mounted\-hook requirement, optimisticData, rollbackOnError and populateCache (vendor; retrieved 2026-10-07T23:15:02Z)

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

Source revision: https://substitutefor.net/entities/Q901?revision=949
