@exodus/swr

中文文档
2.3.7-exodus.7发布于 2026/08/15MITTypeScriptES Module近 30 天下载 1.5万

React Hooks library for remote data fetching

原始使用文档
文档目录4 节
  1. Introduction
  2. Quick Start
  3. Authors
  4. License

SWR


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

import useSWR from 'swr'

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

  if (error) return <div>failed to load</div>
  if (isLoading) return <div>loading...</div>
  return <div>hello {data.name}!</div>
}

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:

Contributors

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


License

The MIT License.

版本与发布记录

@exodus/swr 共发布 9 个版本,首次发布于 2025/11/05;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.3.7-exodus.7latest2026/08/152 个月前
2.3.7-exodus.62025/11/051 年前
2.3.7-52025/11/051 年前
2.3.6-52025/11/051 年前
2.3.6-42025/11/051 年前
2.3.6-32025/11/051 年前

关于 @exodus/swr 的常见问题

@exodus/swr 是什么?

React Hooks library for remote data fetching

如何安装 @exodus/swr?

在项目目录运行 npm install @exodus/swr,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.11.0 || ^17.0.0 || ^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@exodus/swr 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@exodus/swr 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@exodus/swr 可以商用吗?

@exodus/swr 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@exodus/swr 的下载量有多少?

根据 npm 官方下载统计,@exodus/swr 在 2026/10/14 – 2026/11/12 这 30 天内被下载 14,859 次,日均约 495 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@exodus/swr 还在维护吗?

最新版本 2.3.7-exodus.7 发布于 2026/08/15(2 个月前);至今共发布 9 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/11/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。