Skip to content

swr

Use when fetching and caching remote data in React with SWR — useSWR, mutations with optimistic updates, pagination, infinite loading, or automatic revalidation. Also use when choosing between SWR and TanStack Query, or when integrating SWR with Next.js.

ModelSource
sonnetpack: state
Full Reference

┏━ 🔧 swr ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Use when fetching and caching remote data in R… ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

React data fetching library built on the stale-while-revalidate caching strategy. Returns cached data immediately, then revalidates in the background. From Vercel.

Install: npm install swr — v2.x, React 16.8+

ItemValue
Packageswr v2.x
React requirement16.8+
Bundle size~4KB
Provider requiredNo — zero config
DeduplicationAutomatic — same key = one request
Infinite scroll hookuseSWRInfinite from swr/infinite
Explicit mutation hookuseSWRMutation from swr/mutation
Global mutateuseSWRConfig().mutate inside components
I want to…File
Set up useSWR, configure global options, use conditional fetchingreference/core-pattern.md
Do optimistic updates, use useSWRMutation, invalidate cachereference/mutations.md
Build paginated lists or infinite scroll feedsreference/pagination-and-infinite.md
Handle errors, retry logic, Next.js SSR, or compare SWR vs TanStack Queryreference/error-handling-and-nextjs.md

Usage: Read the reference file matching your current task from the index above. Each file is self-contained with code examples and inline gotchas.

  • tanstack-query — More feature-rich alternative; better for complex apps
  • nextjs — App Router SSR patterns work differently with SWR
  • react-hook-form — Forms that submit via useSWRMutation
  • vercel-ai-sdk — Streaming AI responses (different pattern — not SWR)