SW

Swr

swr

React Hooks library for remote data fetching

Hooks
swr
react
hooks
request
cache
fetch
javascript
npm
frontend
open source
Stars
32k
Weekly
12M
Open issues
219
Bundle
~15 kB

Installation

npm install swr

Code example

import useSWR from "swr";

const fetcher = (url: string) => fetch(url).then((r) => r.json());

export function Example() {
  const { data, error, isLoading } = useSWR(
    "https://jsonplaceholder.typicode.com/todos/1",
    fetcher,
  );

  if (isLoading) return <p>Loading…</p>;
  if (error) return <p>Failed to load</p>;
  return <pre>{JSON.stringify(data, null, 2)}</pre>;
}

Pros & cons

Pros

  • Actively maintained by quietshu
  • 12M weekly downloads. Battle tested
  • First-class TypeScript definitions
  • MIT licensed

Considerations

  • Adds roughly ~15 kB to your bundle
  • Needs client-only rendering in SSR frameworks
  • Not supported in React Native
  • 219 open issues on GitHub

FAQs

Alternatives & similar libraries