data-fetching

Implement data-fetching workflows with service layers, Zustand stores, and SWR caching.

Updated Dec 12, 2024
One-click install
npx skills add https://github.com/kingmacth/lobe-chat --skill data-fetching-kingmacth
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: data-fetching
Source: https://github.com/kingmacth/lobe-chat/tree/main/.agents/skills/data-fetching
Command: npx skills add https://github.com/kingmacth/lobe-chat --skill data-fetching-kingmacth

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Data fetching across UI often becomes inconsistent and error-prone without a unified pattern. This guide introduces a service layer, Zustand stores, and SWR to standardize API calls, caching, and state management.

Core Features & Use Cases

  • Service layer abstraction for all API calls
  • Store-based data fetching using SWR hooks
  • Optimistic updates and cache invalidation after mutations
  • Migration guidance from useEffect to dedicated data-fetching patterns
  • Use Case: fetch user lists and related details with predictable cache and revalidation

Quick Start

Configure a new service method, wire it to a store slice via SWR, and call the corresponding useFetch hook in your UI to verify caching and revalidation.

Frequently Asked Questions about data-fetching

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

FAQPage Schema
What is the best way to structure data fetching with Zustand and SWR?

The best way to structure data fetching with Zustand and SWR is to implement a service layer for API calls, wire it to store slices via SWR hooks, and call these hooks in your UI to ensure consistent caching and separation of concerns.

How do I migrate data fetching from useEffect to SWR and Zustand?

To migrate data fetching from useEffect to SWR and Zustand, replace manual effect-based fetching with store-based data hooks, configure a service method, and wire it to a store slice via SWR to handle caching and revalidation automatically.

How do you handle optimistic updates and cache invalidation after API mutations?

You handle optimistic updates and cache invalidation by applying mutations through the service layer, updating the Zustand store optimistically, and triggering SWR revalidation to sync the cache with the latest API state safely.

Can I use this data fetching pattern for list and detail views across web components?

Yes, you can use this data fetching pattern for list and detail views across web components. It implements a clean interface with list/detail patterns, clear mutation flows, and typed params to support scalable extension and safe error handling.

Why do I need a service layer for API calls if I already use SWR for caching?

You need a service layer for API calls alongside SWR to standardize data loading and enforce separation of concerns. This prevents inconsistent fetching logic across UI components by centralizing endpoints before they reach the cache.

What are the limitations of using Zustand stores for data fetching instead of dedicated state libraries?

Using Zustand stores for data fetching requires careful mutation flow management. Without integrating SWR for caching, you may face manual cache invalidation complexities and lose built-in revalidation benefits when scaling API calls.