Fetch, cache, and revalidate data in React with SWR — stale-while-revalidate data fetching library by Vercel. Use when someone asks to "fetch data in React", "SWR", "data fetching hook", "cache API calls", "stale-while- revalidate", "auto-refresh data", or "React data fetching without Redux". Covers data fetching, caching, revalidation, mutation, pagination, and optimistic updates.
SWR (stale-while-revalidate) is a React data fetching library — show cached data instantly, then revalidate in the background. Built by Vercel, it handles caching, deduplication, revalidation on focus/reconnect, pagination, and optimistic updates. Simpler than TanStack Query for straightforward data fetching, with a smaller API surface.
npm install swr
// hooks/useUser.ts — Fetch and cache user data
import useSWR from "swr";
const fetcher = (url: string) => fetch(url).then((r) => r.json());
export function useUser(userId: string) {
const { data, error, isLoading, mutate } = useSWR(
`/api/users/${userId}`,
fetcher,
);
return {
user: data,
isLoading,
isError: error,
mutate, // Manually revalidate
};
}
// Usage in component
function UserProfile({ userId }) {
const { user, isLoading } = useUser(userId);
if (isLoading) return <div>Loading...</div>;
return <div>{user.name}</div>;
}
// app/providers.tsx — Global SWR config
import { SWRConfig } from "swr";
const fetcher = async (url: string) => {
const res = await fetch(url);
if (!res.ok) throw new Error("API error");
return res.json();
};
export function Providers({ children }) {
return (
<SWRConfig
value={{
fetcher,
revalidateOnFocus: true, // Refresh when tab regains focus
revalidateOnReconnect: true, // Refresh when internet reconnects
dedupingInterval: 2000, // Dedupe requests within 2s
errorRetryCount: 3,
}}
>
{children}
</SWRConfig>
);
}
// components/TodoList.tsx — Optimistic updates
import useSWR, { useSWRConfig } from "swr";
function TodoList() {
const { data: todos, mutate } = useSWR("/api/todos");
const addTodo = async (title: string) => {
const newTodo = { id: Date.now(), title, done: false };
// Optimistic update — show immediately, revalidate in background
await mutate(
async () => {
await fetch("/api/todos", {
method: "POST",
body: JSON.stringify({ title }),
});
// Return updated data (or let SWR refetch)
},
{
optimisticData: [...(todos || []), newTodo],
rollbackOnError: true, // Revert if API fails
revalidate: true, // Refetch after mutation
}
);
};
}
// components/PostList.tsx — Paginated data
import useSWR from "swr";
function PostList() {
const [page, setPage] = useState(1);
const { data, isLoading } = useSWR(`/api/posts?page=${page}&limit=20`);
return (
<div>
{data?.posts.map((post) => <PostCard key={post.id} post={post} />)}
<button onClick={() => setPage(page - 1)} disabled={page <= 1}>Previous</button>
<button onClick={() => setPage(page + 1)} disabled={!data?.hasMore}>Next</button>
</div>
);
}
// components/InfiniteFeed.tsx — Infinite scroll
import useSWRInfinite from "swr/infinite";
function InfiniteFeed() {
const { data, size, setSize, isLoading } = useSWRInfinite(
(index) => `/api/feed?page=${index + 1}&limit=20`,
);
const posts = data?.flatMap((page) => page.posts) || [];
const hasMore = data?.[data.length - 1]?.hasMore;
return (
<div>
{posts.map((post) => <PostCard key={post.id} post={post} />)}
{hasMore && (
<button onClick={() => setSize(size + 1)} disabled={isLoading}>
Load More
</button>
)}
</div>
);
}
User prompt: "Build a dashboard that shows live metrics — refresh every 5 seconds."
The agent will use SWR with refreshInterval: 5000, show cached data instantly on mount, and handle loading/error states.
User prompt: "Build a todo app where adding/deleting feels instant."
The agent will use SWR mutations with optimistic data, rollback on error, and automatic revalidation after changes.
null key skips fetching — conditional fetching: useSWR(userId ? /api/... : null)mutate for cache updates — bound (per-key) or globaluseSWRInfinite for infinite scroll — accumulates pagesnpx skills add TerminalSkills/swr下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer