SkillsApr 14, 2026·3 min read

SWR — React Hooks for Data Fetching by Vercel

SWR is a lightweight React data-fetching library that delivers a responsive UX with built-in caching, revalidation, focus tracking, and pagination. Created by Vercel, it powers Next.js apps and any React project that needs stale-while-revalidate semantics.

Agent ready

Review-first install path

This asset needs a review step. The copied prompt tells the agent to dry-run, show the writes, then proceed only after confirmation.

Needs Confirmation · 64/100Policy: confirm
Agent surface
Any MCP/CLI agent
Kind
Skill
Install
Single
Trust
Trust: Community
Entrypoint
step-1.md
Review-first command
npx -y tokrepo@latest install dbec11cd-37be-11f1-9bc6-00163e2b0d79 --target codex

Dry-run first, confirm the writes, then run this command.

TL;DR
SWR provides React hooks for data fetching with caching, revalidation, and focus tracking built in.
§01

What it is

SWR is a lightweight React data-fetching library created by Vercel. It implements the stale-while-revalidate caching strategy: return cached (stale) data instantly, then fetch fresh data in the background, and update the UI when the new data arrives. This delivers a responsive user experience with minimal loading states.

SWR targets React and Next.js developers who need to fetch remote data with smart caching, automatic revalidation on focus/reconnect, pagination, and optimistic updates -- all without a heavy state management library.

§02

How it saves time or tokens

Without SWR, managing server state in React requires useEffect + useState boilerplate, manual cache invalidation, loading/error state tracking, and deduplication of concurrent requests. SWR handles all of this in a single hook call. The useSWR hook returns data, error, and isLoading with zero configuration.

§03

How to use

  1. Install SWR: npm install swr.
  2. Define a fetcher function (e.g., (url) => fetch(url).then(r => r.json())).
  3. Use useSWR(key, fetcher) in any component to fetch and cache data.
§04

Example

import useSWR from 'swr';

const fetcher = (url: string) => fetch(url).then(r => r.json());

function Profile() {
  const { data, error, isLoading, mutate } = useSWR('/api/user', fetcher);

  if (isLoading) return <span>Loading...</span>;
  if (error) return <span>Failed to load</span>;

  return (
    <div>
      <h1>{data.name}</h1>
      <button onClick={() => mutate()}>Refresh</button>
    </div>
  );
}
§05

Related on TokRepo

§06

Common pitfalls

  • Not wrapping the app in SWRConfig for global configuration. Without it, you repeat fetcher and options in every useSWR call.
  • Using SWR for client-side-only data that does not need server revalidation. For local UI state, use useState or a state manager instead.
  • Forgetting that SWR deduplicates requests by key. If two components use the same key, only one fetch fires. This is a feature, but can confuse when debugging.

Frequently Asked Questions

What does stale-while-revalidate mean?+

It is a caching strategy where the cached (stale) data is returned immediately to render the UI, while a background request fetches fresh data. When the fresh data arrives, the UI updates. This eliminates loading spinners for repeat visits while keeping data fresh.

Does SWR work with Next.js Server Components?+

SWR is a client-side hook and runs in Client Components. For Server Components in Next.js App Router, use the native fetch with caching. SWR is best for interactive client-side data that needs revalidation, mutations, and optimistic updates.

How does SWR handle pagination?+

SWR provides `useSWRInfinite` for paginated and infinite scroll patterns. It manages an array of page keys and fetches them incrementally. You control when to load the next page with a `getKey` function that returns the next page URL.

Can SWR do optimistic updates?+

Yes. Use `mutate(key, newData, false)` to update the cache immediately (optimistically) before the server responds. If the server request fails, SWR rolls back to the previous data. This makes the UI feel instant for mutations.

How does SWR compare to React Query (TanStack Query)?+

Both solve the same problem. SWR is lighter and more opinionated, with a simpler API for common cases. TanStack Query provides more features (query invalidation groups, devtools, mutations with built-in retry). Choose SWR for simplicity; choose TanStack Query for complex data-fetching requirements.

Citations (3)
  • SWR GitHub— SWR implements stale-while-revalidate caching for React
  • SWR Documentation— Created by Vercel with built-in focus revalidation and deduplication
  • SWR Pagination Docs— useSWRInfinite for pagination and infinite scroll patterns

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets