software · Q901
SWR
MIT-licensed React data-fetching library with shared caching, revalidation and mutation helpers.
Overview
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
supportedApplication 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
1 source · check evidence
React key/fetcher setup
vendor · checked 2026-10-07Automatic refresh policies
supportedFocus and network reconnect revalidate by default; intervals and immutable behavior are configurable.[4]
Fetching, refreshing and mutating server-backed data in React interfaces · React applications · Version, build and integration requirements apply
1 source · check evidence
Focus/reconnect defaults, intervals and immutable resources
vendor · checked 2026-10-07Custom cache providers
supportedMap-like providers scope cache behavior to an SWRConfig boundary.[6]
Fetching, refreshing and mutating server-backed data in React interfaces · React applications · Version, build and integration requirements apply
1 source · check evidence
Provider boundaries and global mutate limitations
vendor · checked 2026-10-07Optimistic UI and local rollback
supportedMutation helpers can show optimistic data and restore prior local cache on failure; this is not a server transaction rollback.[7]
Fetching, refreshing and mutating server-backed data in React interfaces · React applications · Version, build and integration requirements apply
1 source · check evidence
Mounted-hook requirement, optimisticData, rollbackOnError and populateCache
vendor · checked 2026-10-07Unsuccessful HTTP response detection
limitedThe fetcher must turn unsuccessful status codes into errors; parsed JSON alone does not establish success.[5]
Fetching, refreshing and mutating server-backed data in React interfaces · React applications · Version, build and integration requirements apply
1 source · check evidence
Fetcher rejection, HTTP status checks and data/error coexistence
vendor · checked 2026-10-07Can substitute for
These are products or services this entry can replace. Check the task and limitations for each comparison.
TanStack Query for Fetching, refreshing and mutating server-backed data in React interfaces
limitedCan 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.[2][6][7][8]
React applications · Version, build and integration requirements apply
References
- SWR license
MIT terms
vendor · retrieved 2026-10-07 - SWR getting started
React key/fetcher setup
vendor · retrieved 2026-10-07 - SWR arguments
Entire key identity and changed fetcher context
vendor · retrieved 2026-10-07 - SWR automatic revalidation
Focus/reconnect defaults, intervals and immutable resources
vendor · retrieved 2026-10-07 - SWR error handling
Fetcher rejection, HTTP status checks and data/error coexistence
vendor · retrieved 2026-10-07 - SWR cache providers
Provider boundaries and global mutate limitations
vendor · retrieved 2026-10-07 - SWR mutation and revalidation
Mounted-hook requirement, optimisticData, rollbackOnError and populateCache
vendor · retrieved 2026-10-07 - TanStack Query overview
Server-state caching and query functions
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 |
|---|---|---|
| 949 | 2026-10-13 14:42:02 | /* wbeditentity-create-item:0| */ sf:9d196f80-bd32-49ed-8a61-ab67edfbe06f:v1 | Adds SWR with key identity, fetcher error handling, custom-provider mutation scope and optimistic-cache rollback boundaries. | attribution: /proposals/9d196f80-bd32-49ed-8a61-ab67edfbe06f |