PACKAGE · DATA FETCHING

swr

React Hooks library for remote data fetching

WEEKLY DOWNLOADS 12.6M
STARS 32.4K
FORKS 1.4K
OPEN ISSUES 214
GZIP SIZE 8.7 kB
UNPACKED SIZE 314.9 kB
DEPENDENCIES 4
LAST UPDATED 5mo ago
DOWNLOAD TRENDS

swr downloads — last 12 months

Download trends for swr1 download series from Aug 2025 to Jul 2026. Use left and right arrow keys to inspect monthly values.015.4M30.7M46.1M61.5MAug 2025NovFebMayJul 2026
swr
ABOUT SWR

SWR is a React Hooks library designed to address the complexities of fetching, caching, and synchronizing remote data within React applications. It tackles the common challenge of managing asynchronous data, which often leads to stale UI, complex loading states, and inconsistent data across different components. By providing a unified and declarative approach, SWR simplifies the data fetching lifecycle, making it more predictable and efficient for developers.

The core philosophy behind SWR, which stands for Stale-While-Revalidate, is to always show cached data first, then send the fetch request, and finally update with the fresh data. This strategy ensures a responsive user experience by immediately rendering existing information while background updates occur. This library is primarily aimed at React developers, including those using React Native, who need a robust yet simple solution for managing server state.

Key to SWR's API is the useSWR hook, which takes a unique key (typically a URL) and a fetcher function. It automatically handles caching, revalidation on focus, revalidation on interval, and provides loading and error states. The library also supports mutations through its mutate function, allowing for optimistic UI updates and cache invalidation, enhancing the overall data management capabilities.

SWR integrates seamlessly into the React ecosystem, particularly with Next.js, where it is developed and maintained by Vercel. It's designed to work well with various data fetching strategies, including fetching data from REST APIs, GraphQL endpoints, or even local data sources. Its Suspense mode compatibility further enhances integration with modern React rendering patterns.

With a bundle size of only 8.7 kB (gzipped), SWR offers significant functionality without a substantial performance penalty. The library's maturity is reflected in its extensive adoption, with over 12.6 million weekly downloads, indicating a stable and well-tested solution for remote data fetching challenges.

While SWR is powerful, it's primarily focused on remote data fetching and cache management. For purely local component state, React's built-in `useState` or `useReducer` hooks might be simpler and more appropriate. Additionally, developers must ensure their fetcher functions are correctly implemented to avoid unintended side effects or performance issues.

WHEN TO USE
  • When you need to fetch and cache remote data for React components using a declarative hook-based API.
  • When implementing a stale-while-revalidate caching strategy to improve perceived performance and UI responsiveness.
  • When managing data synchronization across multiple components that consume the same remote data source.
  • When leveraging Next.js and want a tightly integrated data fetching solution.
  • When you require automatic revalidation on window focus, network reconnect, or at configurable intervals.
  • When building applications that benefit from Suspense-mode data fetching for smoother loading states.
WHEN NOT TO USE
  • If you only need simple key-value state management within a single component — React's built-in `useState` is more suitable.
  • If your data fetching requirements are extremely basic and do not involve caching or automatic revalidation.
  • If you are managing complex, deeply nested application state that is not primarily derived from remote sources — dedicated state management libraries might be a better fit.
  • If you prefer a runtime data fetching solution without any client-side caching, a basic `fetch` call wrapped in `useEffect` might suffice.
  • When dealing with strictly static data that never changes and does not require fetching after initial load.

CORRECTIONS

Spot wrong data here?

A short note helps us fix it.

Anonymous · No account · No email back

COMPARISONS 2
swr vs @tanstack/react-query ★ 50.1K · 52.0M/wk swr vs apollo-client ★ 19.8K · 452.5K/wk