taro-swr
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

SWR


fork SWR

有问题邮件dengqing@goldenpig.com.cn

将会和 SWR 保持同步更新,目前版本同步swr v1.0.0

taro-swr Introduction

原则上支持 taro 打包的 h5,暂时只测试过微信小程序环境

基于 swr 改造,有关新的配置变更如下:

  • revalidateOnFocus 只允许在 taro 打包的 h5 环境中使用
  • revalidateOnShow 页面显示/切入前台时触发请求(使用 useDidShow 实现)
  • revalidateOnReconnect 改用 Taro.onNetworkStatusChange 实现,使用方式和原版无区别
  • revalidateOnPullDown 下拉刷新的重新发起请求

除上述所说外和 swr 基本保持一致,直接看官方文档使用。使用示例看/examples/taro

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 } = useSWR('/api/user', fetcher)

  if (error) return <div>failed to load</div>
  if (!data) 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 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 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.

Readme

Keywords

none

Package Sidebar

Install

npm i taro-swr

Weekly Downloads

28

Version

1.0.0

License

MIT

Unpacked Size

314 kB

Total Files

47

Last publish

Collaborators

  • gp-dengqing