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. SWRfirst 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, lightweightand reusabledata fetching
Transport and protocol agnostic
Built-in cacheand request deduplication
Revalidation on focus
Revalidation on network recovery
Pagination and scroll position recovery
Local mutation (Optimistic UI)
Built-in smart error retry
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 fastand reactive.

View full documentation and examples on

Quick Start

  1. ``` js
  2. import useSWR from 'swr'

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

  5.   if (error) return <div>failed to load</div>
  6.   if (!data) return <div>loading...</div>
  7.   return <div>hello {}!</div>
  8. }
  9. ```

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 2 values: data and error. When the request (fetcher) is not yet finished, data will be undefined. And when we get a response, it sets data and error based on the result of fetcher 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


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

Shu Ding (@shuding_ ) – Vercel
Guillermo Rauch (@rauchg ) – Vercel
Joe Haddad (@timer150 ) - Vercel
Paco Coursey (@pacocoursey ) - Vercel


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


The MIT License.