Skip to content
Agents tracked: 506 Releases (7d): 634 Agent status: 4 with issuesSince last limit reset: Claude 15d · Codex 0d For AI agents: Markdown Updated Nov 26, 2026

SWR

React hooks library for data fetching with caching, revalidation and real-time updates.

State and data fetchingNPM

GitHubWebsite

1. License

License from npm.

github.com/vercel/swr

From 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

77 / 100
Gid Score, #148 overall
28.5M
downloads a week, npm
32.5K
GitHub stars
51
agent projects use it
SWR: downloads per weeknpm, swr 28,540,199
SWR: GitHub starsvercel/swr 32,493

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

32 projects, 1.3%
26 projects, 0.8%
18 projects, 0.6%

Often used with

In 88.2% of its projects
In 80.4% of its projects
In 78.4% of its projects
In 100.0% of its projects
In 74.5% of its projects
In 68.6% of its projects
In 68.6% of its projects
In 90.2% of its projects

SWR in practice

Strengths

Bring your own fetcher

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.

Repeat calls to the same key are collapsed

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.

Refetches on focus and reconnect without setup

<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.

Server prefetch that skips the duplicate client request

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

Hooks are client-only

<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.

Server prefetch is experimental

The <code>preload</code> + <code>cacheData</code> pattern needs a recent version and is flagged as experimental in the docs.

cacheData goes on SWRConfig only

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
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

MITGid 7728.5M/wk

TanStack Query

Hooks for managing, caching and synchronizing remote data in React.

MITGid 8892M/wk

Compare SWR with another tool

Get started

Install it.

npm install swr

Rules for your agent

Paste into AGENTS.md or CLAUDE.md
## 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()`.