data-fetching-architecture

Implements SWR-based data fetching with service layers, cache keys, and async error handling in React stores.

Updated Jan 27, 2026
One-click install
npx skills add https://github.com/SmallAi-API/smaihub --skill data-fetching-architecture-smallai-api
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: data-fetching-architecture
Source: https://github.com/SmallAi-API/smaihub/tree/main/.agents/skills/data-fetching-architecture
Command: npx skills add https://github.com/SmallAi-API/smaihub --skill data-fetching-architecture-smallai-api

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? React applications often suffer from scattered API calls, inconsistent cache invalidation, useEffect-based fetching, and loading states that cannot distinguish errors from empty results. This Skill enforces a layered data fetching architecture so reads, writes, and cache refreshes stay consistent across components, stores, and services. ## Core Features & Use Cases - Layered Fetching Architecture: Routes all API calls through service modules and store-level SWR hooks, keeping components free of direct client calls and useEffect fetching. - Cache Key and Invalidation Discipline: Defines shared key factories, matching read/refresh keys, and mutation flows that refresh affected lists and details after writes. - Async State Rendering: Uses AsyncBoundary and AsyncError patterns to correctly distinguish loading, first-load errors, settled empty results, and background revalidation failures. - Use Case: When migrating a component that fetches data in useEffect with local state, move the call into a service, expose a useFetchXxx store hook, and render through AsyncBoundary with proper error and retry wiring. ## Quick Start Refactor this component's useEffect fetch into a service call with a store SWR hook and render it through AsyncBoundary with error and retry handling.

Frequently Asked Questions about data-fetching-architecture

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

FAQPage Schema
How do I migrate a useEffect fetch to SWR in React?

Move the API call into a service module, create a store hook using useClientDataSWR with a shared cache key, and sync results into the store via onSuccess. Replace the component's effect and local state with the hook and selectors, then wire error and retry handling.

How do I invalidate SWR cache after a mutation?

Call the service mutation first, then call mutate with the exact same cache key used by the read hook. Refresh every affected list and detail key, and await the refresh when subsequent UI behavior depends on the updated data.

How should React components handle loading and error states with SWR?

Use an AsyncBoundary component that receives the original SWR data, error, and isLoading values. This distinguishes first-load failures from empty results, since undefined data means no successful response while an empty array is a settled result.

When should I use optimistic updates with SWR mutations?

Use optimistic updates only for create and update operations, with reducer-based state that can be restored or revalidated if the service call fails. Deletes should happen only after server success, never optimistically.

Why does my SWR cache show stale data after switching parent ids?

Stale data occurs when the cache key does not include every parameter that changes the response, such as parent id, filters, or pagination. Ensure read and refresh construct identical keys and capture the request's parent id in callbacks rather than reading current selection.