Skip to content

SWR

A React hook library for fetching data, with caching and automatic refresh.

Stars
32.5k
Forks
1.4k
Licence
MIT
Language
TypeScript
Last update
20 days ago
Type
Library

What it is

SWR is a React Hooks library for loading data in web interfaces. Its name comes from stale-while-revalidate: it shows cached data first, requests fresh data in the background, then updates the screen when the new data arrives. Developers use it to simplify data loading logic in React components.

  • Load a user profile from an API with a single useSWR call that returns data, error, and loading state
  • Show cached content immediately while refreshing it in the background
  • Poll an endpoint at intervals to keep a view up to date
  • Apply optimistic updates to local data before the server responds
  • Paginate lists and recover scroll position

Great for

  • React developers who fetch data from APIs in components
  • Projects that want built-in caching and request deduplication
  • Apps that need data to refresh on focus, network recovery, or polling
  • Teams building React Native or Next.js interfaces

Not ideal for

  • People who do not write React code, since the library is built around React Hooks
  • Projects that need a full server-side data layer rather than client-side fetching hooks
  • Beginners with no JavaScript or React experience, who would need to learn those first
Step-by-step guide: how to use SWRInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Install with a package manager

    You'll need: Node.js and a package manager such as npm · React

    Install the package

    npm install swr
  2. 2

    Run it

    • Install swr in a React project.
    • Import useSWR from 'swr' in a component.
    • Pass a unique key (often an API URL) and a fetcher function.
    • Use the returned data, error, and isLoading values to render the UI.
  3. 3

    Get the most out of it

    • Read the full documentation at swr.vercel.app for options such as polling, retry, and pagination
    • Use the key as a unique request identifier so identical requests are deduplicated
    • Use the fetcher function to plug in your preferred data-fetching library
    • Review the SSR and SSG support described in the README if you use server rendering

Downloads

Strengths

  • Small, focused API: one hook covers the common fetch, loading, and error cases
  • Built-in cache and request deduplication reduce repeated network calls
  • Works with any asynchronous fetcher, so you can use your preferred fetching library

Trade-offs

  • Requires React knowledge; it is not useful outside React-style apps
  • The README is brief and points to the external documentation site for details and examples
  • Behavior such as caching and revalidation has settings that take time to learn

How it compares

ProjectStarsTypeLevelUpdatedBest for
swr (this)32.5kLibraryintermediate20 days agoReact developers who fetch data from APIs in components
query50.5kLibraryintermediate4 days agoFrontend developers building apps that load data from a server
apollo-client19.8kLibraryintermediate5 days agoDevelopers building React apps that query a GraphQL API
usehooks11.5kLibrarybeginner1.5 years agoReact developers who want ready-made hooks for common browser tasks
NativeBase20.4kLibraryintermediate9 months agoTeams maintaining an existing app already built on NativeBase
next.js143kFrameworkintermediate4 days agoDevelopers already comfortable with React who want a full framework around it

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.