TopGit
GitHub Repo Review

SWR: React Hooks for Data Fetching

vercel/swr
STopGit review image for vercel/swr
Review by Topgit.dev for vercel/swr, with GitHub repository stats and README context.
Quick verdict

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.

Stars
โ˜… 32.5k
Forks
โ‘‚ 1.4k
Contributors
๐Ÿ‘ฅ 201
Language
TypeScript
License
MIT
Topic
Frontend
Updated
Oct 2026

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.
How this repository's GitHub stars have grown over time. Source: star-history.com.View the star history โ†—

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.
View on GitHub โ†—Homepage โ†—

Alternatives to SWR

React Query (TanStack Query) โ€” a broader data-fetching/state library for React covering similar stale-while-revalidate style caching with a larger configuration surface.Apollo Client โ€” the better fit if your API is GraphQL and you want queries, mutations, and a normalized cache understood natively instead of hand-written fetchers.RTK Query โ€” worth a look if the project already runs Redux Toolkit and you'd rather keep data fetching inside the same store.

Frequently asked questions

Is SWR free and open source?

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.

Does SWR work with React Native?

SWR works with React Native - the README lists React Native support as one of its built-in capabilities alongside standard React Hooks.

Can I use SWR with TypeScript?

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.

Does SWR support server-side rendering?

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.

What is the difference between SWR and React Query?

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.

How does SWR cache data?

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

Source & attribution

Facts and feature list sourced from the vercel/swr GitHub repository (github.com/vercel/swr) and its README.

GitHub data ยท last synced Aug 15, 2026Reviewed by Henry
โ† Back to TopGit

Want a second opinion on swr?

Ask an AI that can read this page โ€” one click and you get its take on swr.

GitHub