swr

Implements client-side data fetching, caching, and revalidation in React using SWR hooks.

Updated Jul 31, 2026
One-click install
npx skills add https://github.com/AarnavBaddam/skills --skill swr-aarnavbaddam
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: swr
Source: https://github.com/AarnavBaddam/skills/tree/main/swr
Command: npx skills add https://github.com/AarnavBaddam/skills --skill swr-aarnavbaddam

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires swr.

What problem does it solve? Managing server state in React apps—fetching, caching, revalidating, and mutating remote data—requires repetitive boilerplate and careful handling of loading, error, and stale-data states. This Skill provides expert guidance on the SWR library so you can implement the stale-while-revalidate pattern correctly from the start. ## Core Features & Use Cases - Data Fetching with useSWR: Fetch and cache remote data with automatic deduplication, revalidation on focus/reconnect, and error retry with exponential backoff. - Mutations & Optimistic UI: Use useSWRMutation with optimisticData, rollbackOnError, and populateCache for responsive update flows. - Pagination & Real-Time: Handle infinite loading with useSWRInfinite and live data streams with useSWRSubscription for WebSockets or SSE. - Use Case: You are building a dashboard that polls an API every few seconds, shows optimistic updates when users edit records, and paginates a long list—configure SWRConfig once and let the hooks handle caching and revalidation. ## Quick Start Ask the AI to build a React component that fetches user data from an API endpoint using SWR with loading and error states.

Frequently Asked Questions about swr

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fetch data in React with SWR?

Call useSWR with a unique key (usually the API URL) and a fetcher function that returns a promise. The hook returns data, error, isLoading, isValidating, and mutate, and automatically caches and deduplicates requests sharing the same key.

How do I handle mutations and optimistic updates in SWR?

Use useSWRMutation from swr/mutation with a trigger function to perform remote updates. Pass optimisticData, rollbackOnError, populateCache, and revalidate options to update the UI instantly and roll back if the request fails.

Does SWR support pagination and infinite loading?

Yes, useSWRInfinite from swr/infinite handles paginated and infinite-scroll data. Provide a getKey function that returns the URL per page index (or null to stop), then call setSize to load additional pages.

How do I skip fetching conditionally with useSWR?

Pass null or a falsy value as the key to skip fetching, such as useSWR(userId ? `/api/user/${userId}` : null, fetcher). Never call the hook conditionally; use conditional keys instead to comply with React hook rules.

Why does SWR keep retrying failed requests?

SWR retries on error by default using exponential backoff. Customize behavior with the onErrorRetry option to skip retries for specific status codes like 404 or to cap the maximum retry count.

Can SWR subscribe to WebSockets or real-time data?

Yes, useSWRSubscription from swr/subscription connects to real-time sources like WebSockets or SSE. The subscribe function receives a next(error, data) callback and must return a cleanup function; multiple components with the same key share one subscription.