swr downloads · last 12 months
SWR is a React Hooks library for fetching remote data. It addresses the recurring work around loading and refreshing server data in React components by providing a hook-based interface and a cache-oriented, stale-while-revalidate approach.
Its design centers on keeping remote data available while a revalidation runs, rather than treating each request as an isolated fetch. This suits React developers who want components to read remote data through hooks and coordinate updates through SWR’s caching behavior; it does not replace the need to define application-specific endpoints, request options, or server-side behavior.
The main API pattern is `useSWR`, which accepts a key identifying the requested data and a fetcher that retrieves it, then exposes request data and status to the component. Keys can represent request inputs, and the cache can support reuse and revalidation; applications can also use mutation patterns to update remote data and keep the displayed result in sync.
SWR is aimed at React applications, with package topics also identifying Next.js and React Native. Its hook-based integration fits component-driven interfaces where remote data needs to be read or refreshed, including workflows that use Suspense; choose request keys and fetcher behavior to match the application’s data and authentication requirements.
The supplied package information lists version 2.5.1 under the MIT license, with a gzip bundle size of 8.9 kB and an unpacked size of 320.3 kB. It also reports 7.9M weekly downloads, 32.5K GitHub stars, 1.4K forks, 241 open issues, and a last-updated date of 2026-10-16; these figures indicate activity but do not guarantee compatibility with a particular React setup.
SWR focuses on remote data fetching, so it is not a general replacement for local component state or a complete API client. Its cache and revalidation behavior also means developers should make request keys stable and account for when cached data may be shown while fresh data is being requested.
- Use `useSWR(key, fetcher)` to load remote data directly in React components and expose the result to the interface.
- Use a stable key that includes request inputs when the component’s remote data depends on values such as an identifier or query.
- Use SWR’s stale-while-revalidate behavior when the interface should keep cached data available while requesting an update.
- Use SWR’s cache and revalidation patterns when multiple component reads should reuse remote data instead of each starting an unrelated request.
- Use mutation patterns when an action changes remote data and the UI should update or revalidate the corresponding cached result.
- Consider SWR in React, Next.js, or React Native projects where a hook-based remote-data interface fits the rendering workflow.
- Consider its Suspense-related ecosystem fit when the React application is organized around Suspense for remote-data loading.
- Avoid SWR when the data is only local UI state; React’s built-in `useState` or `useReducer` is a more direct fit.
- Avoid `useSWR` if the application needs imperative request orchestration outside React components; use a fetcher or request layer directly and connect results to the UI as needed.
- Avoid relying on SWR alone for complex API-client concerns such as endpoint construction, authentication, or custom request policies; implement those in the fetcher or a dedicated application layer.
- Avoid stale-while-revalidate display behavior if the interface must never show cached data before a fresh response; use an explicit request flow that waits for the new result.
- Avoid SWR if the project does not use React Hooks; its primary interface is a React Hooks library, so use a framework-appropriate data-fetching mechanism.
- Avoid treating SWR as a server-side data store or persistence layer; keep durable data and business rules on the server and use SWR for client-facing remote reads and updates.
CORRECTIONS
Spot wrong data here?Spot wrong data on this page?
A short note helps us fix it.A short note helps us fix it. We read every one; confirmed fixes ship in the next nightly build.
Anonymous · No account · No email back