SWR: React Hooks for Data Fetching
SWR does one job well: keep a React component's data in sync with the server without hand-writing fetch/cache/retry logic. The `useSWR` hook replaces the copy-pasted `useState`+`useEffect` pattern, and revalidation on focus or network recovery catches staleness a plain fetch-on-mount misses. It's narrower than a GraphQL client like Apollo - no query language, no normalized cache - so treat it as a caching layer, not a full framework.
What is SWR
SWR is a React Hooks library for data fetching, built around the stale-while-revalidate caching strategy from HTTP RFC 5861. The name comes directly from that strategy: a component gets cached (stale) data immediately, SWR fires the real request in the background, and the UI updates once fresh data arrives. It ships as a single `useSWR` hook returning `data`, `error`, and `isLoading`.
Key features of SWR
- โStale-while-revalidate caching: SWR serves cached data immediately, then revalidates the request in the background and updates the UI when fresh data lands.
- โAutomatic request deduplication - multiple components asking for the same key share one network call instead of firing duplicate requests.
- โRevalidation on window focus and on network reconnect, plus optional polling.
- โPagination and scroll position recovery for list-based UIs.
- โLocal mutation for optimistic UI updates, plus built-in smart error retry.
- โFirst-class TypeScript types, React Suspense support, and a React Native build in the same package.
- โTransport and protocol agnostic - the `fetcher` function can be `fetch`, `axios`, GraphQL, or anything else you already use.
Common use cases for SWR
- โขDashboards or live counts where the UI should refresh itself when the user refocuses the tab, without a manual refresh button.
- โขPaginated or infinite-scroll lists where scroll position needs to survive a background revalidation.
- โขNext.js apps doing SSR/SSG that still need a client-side refresh after the page hydrates.
- โขOptimistic UI - updating the local cache the instant a user acts (e.g. toggling a like) before the server confirms it, via local mutation.
- โขPages where several components request the same endpoint and you don't want each one firing its own network call.
Installing SWR in your project
SWR's own README doesn't walk through installation step by step, but it confirms the package publishes to npm under the name `swr`. Install it the way you'd install any npm package: `npm install swr` (or `yarn add swr`, `pnpm add swr`). No separate CLI, config file, or account setup - you import `useSWR` from `swr` and start calling it inside components, the same as any other npm dependency in a React project.
Basic useSWR hook example
The Quick Start example in the README shows the whole pattern in a few lines: ```js import useSWR from 'swr' function Profile() { const { data, error, isLoading } = useSWR('/api/user', fetcher) if (error) return failed to load if (isLoading) return loading... return hello {data.name}! } ``` `useSWR` takes two arguments: a `key` (usually the request URL, used to identify and cache the request) and a `fetcher`, an async function that receives that key and resolves with the data. The hook returns `data`, `error`, and `isLoading` - while the request is in flight `data` is `undefined` and `isLoading` is `true`; once the fetcher resolves, SWR sets `data` or `error`, flips `isLoading` to `false`, and re-renders the component. The fetcher itself isn't SWR's concern - use `fetch`, `axios`, or whatever your project already calls to hit the API.
Advantages of using SWR
- โOne hook (`useSWR`) replaces the `useState`+`useEffect`+loading-state boilerplate most teams rewrite in every component.
- โCache and request dedup ship built in - not a second package to wire up.
- โTransport agnostic: pair it with `fetch`, `axios`, or a GraphQL client since SWR only cares about the key and the fetcher function you give it.
- โTypeScript, React Suspense, and React Native support all live in the same package.
- โBuilt by the Next.js team, so SSR/SSG behavior is a first-class concern, not a bolt-on.
SWR limitations and tradeoffs
- โณThe material here has no step-by-step install or config walkthrough - that lives on the separate docs site, not in this README.
- โณSWR only fetches and caches; it doesn't ship a query language or a normalized store the way a GraphQL-specific client does, so you still write your own `fetcher` and any POST/PATCH calls.
- โณNo devtools or cache-inspector panel shows up in this material, so seeing what's actually cached usually means logging it yourself.
- โณThe Quick Start example is the only usage pattern given here - things like conditional fetching or dependent queries aren't covered in what's provided.
Alternatives to SWR
Frequently asked questions
SWR is free and open source, released under the MIT license, so you can use it in commercial and personal React projects without licensing fees.
SWR works with React Native - the README lists React Native support as one of its built-in capabilities alongside standard React Hooks.
SWR can be used with TypeScript - the project lists TypeScript support directly among its core features, so hook return values like `data` and `error` come typed without installing extra packages.
SWR supports server-side rendering and static generation (SSR and SSG), which the README lists explicitly - useful since the library comes from the team that also builds Next.js.
The core difference is scope: SWR is a lightweight hook built specifically around the stale-while-revalidate caching pattern, while React Query (TanStack Query) is a larger library with a broader feature set for managing server state. Both handle React data fetching, but SWR's own docs frame it as fast and lightweight rather than a full toolkit.
SWR caches data by key - typically the request URL - and serves that cached value immediately on a repeat call before firing a fresh request in the background, then updates the cache and the component once new data returns, following the stale-while-revalidate strategy its name is built on.
The problem it solves
Every React app fetches data somehow. Most components end up rewriting the same `useState` + `useEffect` + loading/error dance, and naive versions of that pattern don't dedupe repeat requests, don't refetch when a user tabs back in, and lose scroll position on paginated lists when data reloads. SWR folds that stale-while-revalidate caching strategy - defined by HTTP RFC 5861 - into one hook so components stop reinventing it.
Who should try it โ and who should skip
Try SWR if you're building a React (or Next.js) app that needs data to stay fresh without hand-rolling `useEffect` plus loading/error state for every fetch - it's especially natural if you're already on Next.js, since the same team builds both. Skip it if your API is GraphQL-heavy and you want a client that understands queries, mutations, and normalized caching out of the box, since that's Apollo Client's job, not SWR's, or if your framework already has a loader pattern and you don't need extra revalidation behavior on top.
Related repositories
Want a second opinion on swr?
Ask an AI that can read this page โ one click and you get its take on swr.
