data-fetching-patterns

Standardize SWR-based API data fetching with typed fetchers and custom hooks.

2|1|Updated Jan 7, 2021
One-click install
npx skills add https://github.com/lsst-sqre/squareone --skill data-fetching-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: data-fetching-patterns
Source: https://github.com/lsst-sqre/squareone/tree/main/.claude/skills/data-fetching-patterns
Command: npx skills add https://github.com/lsst-sqre/squareone --skill data-fetching-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires react, swr, next.js.

What problem does it solve?

Managing data fetching, caching, loading states, and error handling in modern web applications can be complex and lead to inconsistent user experiences. This skill provides a comprehensive guide to SWR-based data fetching patterns, ensuring efficient, robust, and consistent API interactions across the monorepo.

Core Features & Use Cases

  • SWR-Powered Data Management: Covers basic SWR usage, custom hook patterns, and advanced configurations for caching, revalidation, polling, and infinite loading.
  • Robust Error & Loading States: Guides on implementing graceful loading indicators, comprehensive error handling in components, and global error management.
  • Mock Data & Mutations: Explains how to set up mock data for development, implement optimistic updates, and manage data revalidation after mutations.
  • Use Case: When building a new feature that displays data from an API, use this skill to create a custom SWR hook, implement skeleton loaders for loading states, and provide clear error messages if the fetch fails.

Quick Start

Create a custom SWR hook to fetch user information from /auth/api/v1/user-info. import { useUserInfo } from './hooks/useUserInfo'; const { user, isLoading, error } = useUserInfo();

Frequently Asked Questions about data-fetching-patterns

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

FAQPage Schema
How do I handle loading and error states when fetching data from an API?

Loading and error states in API data fetching can be managed using SWR, which provides `isLoading` and `error` properties out of the box. Implement skeleton loaders during the loading phase and display user-friendly error messages when the fetch fails, ensuring consistent UX across your application.

What's the best way to implement caching and revalidation for API calls?

SWR handles caching and revalidation automatically through configurable strategies. Set cache duration, enable polling for real-time updates, and control revalidation on focus or interval to balance freshness with performance, reducing unnecessary API calls across your application.

How do I create reusable custom hooks for data fetching in React?

Custom SWR hooks like `useUserInfo` encapsulate API fetcher logic, configuration, and data transformation in a single function. This pattern standardizes data fetching across components, improves testability, and enables consistent error and loading state handling throughout your codebase.

Can I use SWR with Next.js for server-side and client-side data fetching?

Yes, SWR integrates seamlessly with Next.js for client-side data fetching. Combine it with Next.js API routes or external APIs, and leverage Next.js features like incremental static regeneration for optimal performance and caching strategies.

How do I implement optimistic updates and handle data revalidation after mutations?

Optimistic updates allow UI changes before server confirmation, improving perceived performance. After mutations, use SWR's revalidation controls to refresh cached data, ensuring consistency between client state and server truth without full page reloads.

What's the advantage of using SWR over direct fetch or axios calls?

SWR provides built-in caching, automatic revalidation, deduplication, and error handling without boilerplate. It reduces API calls, simplifies loading and error state management, and enables advanced features like polling and infinite loading with minimal configuration overhead.