# swr

> React Hooks for Data Fetching

- Type: agent or tool
- Field: Web & Frontend
- Repository: https://github.com/vercel/swr
- GitHub stars: 32495
- Licence: MIT
- Language: TypeScript
- Last push: 2026-10-16
- Page: https://imutechz.in/agents/swr

## What it is
SWR is a React Hooks library for data fetching that implements the stale-while-revalidate strategy. It returns cached data immediately, then revalidates in the background, and updates the UI with fresh data. The library is transport-agnostic, works with React and React Native, and includes built-in caching, deduplication, and automatic revalidation on focus or network recovery.

## Features
- stale-while-revalidate caching strategy
- built-in cache and request deduplication
- revalidation on focus and network recovery
- pagination and scroll position recovery
- SSR and SSG support
- local mutation with optimistic UI

## Best for
- fetching data in React applications
- real-time UI updates with automatic revalidation
- optimistic UI updates
- server-side rendered or static sites

## Pros
- lightweight and reusable data fetching
- transport and protocol agnostic
- smart error retry built-in
- works with any async fetcher function

## Cons
- requires a fetcher function to be provided
- cache key must be unique per request

## Usage
```
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>
}
```

Tags: react, hooks, data-fetching, caching, swr, vercel, typescript, react-native
