GitXplorerGitXplorer
v

swr

public
30269 stars
1212 forks
135 issues

Commits

List of commits on branch main.
Unverified
1585a3e37d90ad0df8097b099db38f1afb43c95d

2.2.6-beta.4

vvercel-release-bot committed 4 months ago
Verified
b2126a6cf7df4be535851f2a1560d8478e9c4f7d

fix: Improve comparison performance (#2973)

sshuding committed 4 months ago
Unverified
682039a43bc0d8776e609eb5f9b6eeb9f3db91ea

2.2.6-beta.3

vvercel-release-bot committed 4 months ago
Verified
fedf233a48d40d1cd96517941b5f9e15ff2fc3ab

chore: add react peerDeps 19 (#2963)

ddevjiwonchoi committed 4 months ago
Verified
cb2946ddcafbeedecf724aa19b3929865c026bc7

fix(infinte): export SWRInfiniteKeyedMutator type (#2900)

LLeoMcA committed 4 months ago
Verified
2a5000ed29aa12d000c6f1dc017672dac3044f60

fix: check if config.fallback is undefined (#2913)

ttaku-hatano committed 4 months ago

README

The README file for this repository.

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.