MCPcopy
hub / github.com/vercel/swr

github.com/vercel/swr @v2.4.2 sqlite

repository ↗ · DeepWiki ↗ · release v2.4.2 ↗
543 symbols 1,527 edges 253 files 4 documented · 1%
README

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 

failed to load


  if (isLoading) return 

loading...


  return 

hello {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.

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.

Extension points exported contracts — how you extend this code

SWRGlobalConfig (Interface)
(no doc)
test/type/suspense/helper-types.tsx
SWRInfiniteCompareFn (Interface)
(no doc)
src/infinite/types.ts
SWRGlobalConfig (Interface)
(no doc)
src/index/index.ts
InternalConfiguration (Interface)
(no doc)
src/_internal/types.ts
TriggerWithArgs (Interface)
(no doc)
src/mutation/types.ts
SWRGlobalConfig (Interface)
(no doc)
examples/suspense-global/global-swr-config.tsx
SWRGlobalConfig (Interface)
(no doc)
test/type/suspense/suspense.ts
SWRInfiniteConfiguration (Interface)
(no doc)
src/infinite/types.ts

Core symbols most depended-on inside this repo

createKey
called by 342
test/utils.tsx
renderWithConfig
called by 285
test/utils.tsx
sleep
called by 245
test/utils.tsx
createResponse
called by 212
test/utils.tsx
expectType
called by 137
test/type/utils.ts
truthy
called by 55
test/type/utils.ts
useSWRConfig
called by 50
src/_internal/utils/use-swr-config.ts
preload
called by 45
src/_internal/utils/preload.ts

Shape

Function 506
Interface 20
Method 9
Class 8

Languages

TypeScript100%

Modules by API surface

test/type/config.tsx19 symbols
src/index/use-swr.ts16 symbols
test/utils.tsx15 symbols
test/use-swr-local-mutation.test.tsx14 symbols
test/use-swr-infinite.test.tsx12 symbols
src/_internal/types.ts12 symbols
test/type/fetcher.ts10 symbols
test/use-swr-integration.test.tsx9 symbols
test/type/trigger.ts9 symbols
test/type/option-fetcher.ts9 symbols
test/use-swr-cache.test.tsx8 symbols
test/use-swr-subscription.test.tsx7 symbols

Dependencies from manifests, versioned

@arethetypeswrong/cli0.18.2 · 1×
@edge-runtime/jest-environment4.0.0 · 1×
@eslint/compat2.0.0 · 1×
@eslint/eslintrc3.3.1 · 1×
@eslint/js9.39.1 · 1×
@playwright/test1.57.0 · 1×
@reach/combobox0.16.1 · 1×
@swc/core1.15.3 · 1×
@swc/jest0.2.39 · 1×
@testing-library/dom10.4.1 · 1×
@testing-library/jest-dom6.9.1 · 1×
@testing-library/react16.3.0 · 1×

For agents

$ claude mcp add swr \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact