AgentSkillsCN

data-fetching

前端应用的数据获取、缓存与失效模式。关键词:数据获取、API、缓存、查询、变更、React Query。

SKILL.md
--- frontmatter
name: data-fetching
description: "Data fetching, caching, and invalidation patterns for frontend apps. Keywords: data fetching, api, caching, query, mutation, react query."

Data Fetching

This skill describes patterns for client-side data fetching and caching.


1. Core rules

  1. Prefer a single data access layer (API client + feature APIs).
  2. Use stable query keys for caching.
  3. Invalidate caches intentionally on mutations.
  4. Keep loading/error UX consistent (Suspense or explicit states).

2. Query key pattern

Example shape:

  • ["users", "list", filters]
  • ["users", "detail", userId]

Avoid:

  • keys that depend on unstable object identity
  • mixing unrelated resources in a single key

3. Mutation pattern

After a successful mutation:

  • invalidate affected list queries
  • update detail caches when possible

4. Error handling

  • Show user-friendly errors.
  • Capture exceptions to monitoring with route/feature context (when available).