Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vercel/swr
React Hooks for Data Fetching
https://github.com/vercel/swr
cache data data-fetching fetch hook hooks nextjs react react-native stale-while-revalidate suspense swr vercel
Last synced: 3 days ago
JSON representation
React Hooks for Data Fetching
- Host: GitHub
- URL: https://github.com/vercel/swr
- Owner: vercel
- License: mit
- Created: 2019-10-28T18:16:01.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-07-11T17:05:52.000Z (5 months ago)
- Last Synced: 2024-10-30T07:00:07.711Z (about 1 month ago)
- Topics: cache, data, data-fetching, fetch, hook, hooks, nextjs, react, react-native, stale-while-revalidate, suspense, swr, vercel
- Language: TypeScript
- Homepage: https://swr.vercel.app
- Size: 3.23 MB
- Stars: 30,477
- Watchers: 218
- Forks: 1,217
- Open Issues: 142
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: .github/SECURITY.md
Awesome Lists containing this project
- awesome - vercel/swr - React Hooks for Data Fetching (TypeScript)
- awesome-nextjs - Swr - React Hooks for Data Fetching. (Officially for Nextjs)
- awesome-github-repos - vercel/swr - React Hooks for Data Fetching (TypeScript)
- awesome-libraries - swr - SWR is a React Hooks library for remote data fetching by Vercel. (React / Data fetching)
- awesome-github-star - swr
- awesome - vercel/swr
- awesome-repos - swr
- awesome-web-react - SWR - React Hooks library for data fetching. (Component and Hook Libraries)
- awesome-list - swr
- best-of-react - GitHub - 13% open · ⏱️ 31.05.2024): (Data Fetching)
- StarryDivineSky - vercel/swr - while-revalidate ,由HTTP RFC 5861推广的缓存失效策略。SWR先从缓存中返回数据(过时),然后发送请求(重新验证),最后再次附带最新数据。 (前端开发框架及项目 / 其他_文本生成、文本对话)
- awesome-react-cn - swr
- awesome-react - swr - React Hooks for Data Fetching (**Awesome React** [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome) / React)
- awesome - swr - SWR is a React Hooks library for remote data fetching. (Frontend frameworks & libraries)
- awesome - swr - SWR is a React Hooks library for remote data fetching. (Frontend frameworks & libraries)
- awesome-star-libs - vercel / swr
- awesome-whatthefar - SWR
- vueposu - vercel/swr
- vueposu - vercel/swr
README
[![SWR](https://assets.vercel.com/image/upload/v1572289618/swr/banner.png)](https://swr.vercel.app)
## 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](https://tools.ietf.org/html/rfc5861).
**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](https://swr.vercel.app).**
## Quick Start
```js
import useSWR from 'swr'function Profile() {
const { data, error, isLoading } = useSWR('/api/user', fetcher)if (error) return
failed to load
if (isLoading) returnloading...
returnhello {data.name}!
}
```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](https://swr.vercel.app).**
## Authors
This library is created by the team behind [Next.js](https://nextjs.org), with contributions from our community:
- Shu Ding ([@shuding\_](https://twitter.com/shuding_)) - [Vercel](https://vercel.com)
- Guillermo Rauch ([@rauchg](https://twitter.com/rauchg)) - [Vercel](https://vercel.com)
- Joe Haddad ([@timer150](https://twitter.com/timer150)) - [Vercel](https://vercel.com)
- Paco Coursey ([@pacocoursey](https://twitter.com/pacocoursey)) - [Vercel](https://vercel.com)[Contributors](https://github.com/vercel/swr/graphs/contributors)
Thanks to Ryan Chen for providing the awesome `swr` npm package name!
## License
The MIT License.