SWR
React hooks library for data fetching with caching, revalidation and real-time updates.
3. Used by agents' projects
Dependency in 51 public GitHub projects set up for AI coding agents that we scanned (0.8%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
swr.vercel.app/llms.txtFrom the package registry and GitHub, checked daily. Tap a verdict to see its source.How we checkedReport an error
What SWR does
SWR puts the stale-while-revalidate cache rule behind a single React hook: it returns cached data at once, refetches in the background, then re-renders, and newer versions can seed that cache from React Server Components.
SWR popularity
Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated
How SWR is used
- SWR is a dependency in 51 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (1.0% of the 4,867 JavaScript/TypeScript ones), #5 of 53 in state and data fetching.
- Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 239; those are left out of every share on this page.
- The most used alternative in our scan is TanStack Query (458 projects).
- Projects that use it most often also use Next.js (88.2%), Lucide React (80.4%), clsx (78.4%).
By coding agent
Often used with
SWR in practice
Strengths
SWR does not ship an HTTP client. The README says the fetcher can be any async function. Startup House wrapped its existing axios instance, with interceptors and headers, and used that as the fetcher.
Requests that share a key inside <code>dedupingInterval</code> (2000 ms by default) are deduplicated. A deeply nested child can call the same hook as its parent without firing a second request.
<code>revalidateOnFocus</code> and <code>revalidateOnReconnect</code> both default to <code>true</code>. Polling is opt-in through <code>refreshInterval</code>, which can be a number or a function of the latest data.
In the App Router, <code>preload</code> in a Server Component plus <code>SWRConfig</code>'s <code>cacheData</code> hands server-fetched data to client hooks. The docs say the first client request is then skipped, and later revalidations use the client fetcher.
Watch out for
<code>useSWR</code>, <code>useSWRInfinite</code> and the mutation hook cannot be imported in React Server Components. Only <code>SWRConfig</code>, <code>preload</code> and the key serialization APIs work there.
The <code>preload</code> + <code>cacheData</code> pattern needs a recent version and is flagged as experimental in the docs.
You cannot pass <code>cacheData</code> to an individual hook. Array or function keys also have to be serialized with <code>unstable_serialize</code> when you build <code>cacheData</code> by hand.
Quick start: fetch a user
import useSWR from 'swr'
function Profile() {
const { data, error, isLoading } = useSWR('/api/user', fetcher)
if (error) return <div>failed to load</div>
if (isLoading) return <div>loading...</div>
return <div>hello {data.name}!</div>
}More about SWR
Head to head
- TanStack Query vs SWR used together in 14 projects
Packages around it
- next: React framework
- clsx: className builder
- lucide-react: Icon set
- react: UI library
- tailwind-merge: Tailwind class merging
- @radix-ui: UI primitives
More in State and data fetching
Alternatives
SWR This page
Hooks for managing, caching and synchronizing remote data in React.
Get started
Install it.
npm install swrRules for your agent
## SWR
- Call `useSWR`, `useSWRInfinite` and `useSWRMutation` only in Client Components (`'use client'`). In Server Components, import only `SWRConfig`, `preload` and `unstable_serialize`.
- Use one consistent key per resource, normally the API URL. The same data must always get the same key, and different data must never share a key.
- To prefetch on the server, call `preload(key, fetcher)` and pass the merged results to `<SWRConfig value={{ cacheData }}>`. Never pass `cacheData` to `useSWR`. This requires SWR 2.5.0-beta.1+ and is experimental.
- When building `cacheData` by hand with array or function keys, serialize them with `unstable_serialize`.
- Show spinners from `isLoading`, which is only true with no loaded data. Use `isValidating` for background refreshes, such as disabling a Retry button.
- With `useSWRInfinite`, return `null` from `getKey` when the previous page is empty, to stop fetching.
- Retries are on by default (`shouldRetryOnError = true`). Set it to `false` if the UI offers its own retry via `mutate()`.