Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

swr

> 前端框架
Open source

React Hooks for Data Fetching

32.4K stars0 likes1 views
WebsiteGitHub

About

React Hooks for Data Fetching


Introduction

SWR is a React Hooks library for data fetching.

The name “SWR” is derived from stale-while-revalidate, a cache invalidation strategy popularized by HTTP RFC 5861. SWR first returns the data from cache (stale), then sends the request (revalidate), and finally comes with the up-to-date data again.

With just one hook, you can significantly simplify the data fetching logic in your project. And it also covered in all aspects of speed, correctness, and stability to help you build better experiences:

  • Fast, lightweight and reusable data fetching
  • Transport and protocol agnostic
  • Built-in cache and request deduplication
  • Real-time experience
  • Revalidation on focus
  • Revalidation on network recovery
  • Polling
  • Pagination and scroll position recovery
  • SSR and SSG
  • Local mutation (Optimistic UI)
  • Built-in smart error retry
  • TypeScript
  • React Suspense
  • React Native

...and a lot more.

With SWR, components will get a stream of data updates constantly and automatically. Thus, the UI will be always fast and reactive.


View full documentation and examples on swr.vercel.app.


Quick Start

javascript
import useSWR from 'swr'

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

  if (error) return 

  if (isLoading) return 

  return 

}

In this example, the React Hook useSWR accepts a key and a fetcher function. The key is a unique identifier of the request, normally the URL of the API. And the fetcher accepts key as its parameter and returns the data asynchronously.

useSWR also returns 3 values: data, isLoading and error. When the request (fetcher) is not yet finished, data will be undefined and isLoading will be true. When we get a response, it sets data and error based on the result of fetcher, isLoading to false and rerenders the component.

Note that fetcher can be any asynchronous function, you can use your favourite data-fetching library to handle that part.


View full documentation and examples on swr.vercel.app.


Authors

This library is created by the team behind Next.js, with contributions from our community:

  • Shu Ding (@shuding_) - Vercel
  • Jiachi Liu (@huozhi) - Vercel
  • Guillermo Rauch (@rauchg) - Vercel
  • Joe Haddad (@timer150) - Vercel
  • Paco Coursey (@pacocoursey) - Vercel

Contributors

Thanks to Ryan Chen for providing the awesome swr npm package name!


License

The MIT License.

Issues· 226 open

View all issuesOpen on GitHub
  • #4342

    Focus events emit TimeoutNaNWarning with Node.js timers in jsdom

    Updated Sep 28, 2026
  • #4339

    onSuccess can fire for an obsolete request after the key changes to null

    Updated Sep 25, 2026
  • #4327

    Older Preload state overwrites a more recent mutated cached value

    Updated Sep 7, 2026
  • #2951

    Missing export of type `SWRInfiniteMutatorOptions` starting from 2.2.5

    Updated Sep 1, 2026
  • #4322

    `useSWRImmutable` ignores hook-level `refreshInterval` since 2.4.0 (regression of #4208)

    Updated Aug 31, 2026
  • #4314

    Suspense mode passes an uncached promise to use(), triggering React's uncached-promise warning

    Updated Aug 14, 2026
  • #4179

    Destruct "error" from useSWR throws an error

    reproduction neededUpdated Aug 7, 2026
  • #4180

    SWR re-renders the whole paginated data array when optimisticData and data functions return an array

    Updated Aug 1, 2026
  • #2888

    Type error when overriding global fetcher function with null

    bugUpdated Jul 12, 2026
  • #2771

    Bug in `createCacheHelper`

    reproduction neededUpdated May 19, 2026

> Tags

TypeScriptcachedatadata-fetchingfetch

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 30, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架