Token导航 LogoToken导航TokenDH.com
开发规范需要联网github未标认证来源可访问许可证需确认审计通过

react-query-best-practicesReact query 最佳实践

Agent Skill

用于辅助前端页面、组件、样式和交互逻辑的开发与维护。它适合让 Agent 生成或审查 React、Next.js、Vue、Tailwind、CSS 等相关代码,整理组件结构,或定位布局和性能问题。使用时需要结合项目现有设计系统、路由和构建方式,避免只生成孤立片段;涉及页面改动时,应配合本地预览和构建检查确认视觉效果。

总安装

1,646

周安装

70

GitHub Stars

公开资料未说明

下载量

577
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:react-query-best-practices(React query 最佳实践)
来源仓库:https://github.com/arraydude/agent-skills
仓库路径:skills/react-query-best-practices
安装命令:
npx skills add https://github.com/arraydude/agent-skills --skill react-query-best-practices
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/arraydude/agent-skills --skill react-query-best-practices

简介

总结 React Query 在生产环境中的最佳实践,规避常见陷阱提升稳定性。

  • 适用于已有项目接入 React Query 后的规范化使用与故障预防。
  • 可提供 queryClient 实例管理、并行查询优化及乐观更新实现指南。
  • 需统一团队对 useQuery/useMutation 的使用约定减少认知偏差。
  • 建立监控告警机制,及时发现缓存雪崩或内存溢出等异常状况。

SKILL.md

React Query Best Practices

Important: This guide targets React Query v4. Some patterns may differ in v5.

Comprehensive guide for React Query v4 (TanStack Query) based on TkDodo's authoritative blog series. Contains 24 rules across 7 categories, prioritized by impact.

When to Apply

Reference these guidelines when:

  • Implementing new queries or mutations
  • Integrating WebSockets with React Query
  • Setting up query invalidation patterns
  • Debugging React Query behavior
  • Optimizing render performance
  • TypeScript integration questions

Rule Categories by Priority

PriorityCategoryImpactPrefix
1Query Keys & PatternsCRITICALquery-
2Mutations & UpdatesCRITICALmutation-
3Caching StrategyHIGHcache-
4WebSocket IntegrationHIGHwebsocket-
5TypeScript IntegrationMEDIUMtypescript-
6Testing PatternsMEDIUMtesting-
7Common PitfallsMEDIUMtroubleshoot-
8Migration to v5HIGHmigration-

Quick Reference

1. Query Keys & Patterns (CRITICAL)

  • query-keys-as-dependencies - Include all queryFn params in queryKey
  • query-key-factory - Use factory pattern for consistent key generation
  • query-select-transforms - Use select option for data transformations
  • query-status-check-order - Check data first, then error, then loading
  • query-tracked-properties - Only destructure properties you use
  • query-placeholder-vs-initial - Know when to use each approach
  • query-dependent-enabled - Use enabled option for dependent queries

2. Mutations & Updates (CRITICAL)

  • mutation-prefer-mutate - Use mutate() with callbacks over mutateAsync()
  • mutation-invalidation - Invalidate queries after mutations
  • mutation-direct-cache-update - Update cache directly when appropriate
  • mutation-optimistic-updates - Show success immediately, rollback on failure
  • mutation-callback-separation - Query logic in hook, UI effects in component

3. Caching Strategy (HIGH)

  • cache-stale-time - Set appropriate staleTime for your domain
  • cache-refetch-triggers - Keep refetch triggers enabled in production

4. WebSocket Integration (HIGH)

  • websocket-event-invalidation - Use events to trigger invalidation
  • websocket-stale-time-infinity - Set staleTime: Infinity for WS-managed data
  • websocket-reconnection - Invalidate stale queries on reconnect

5. TypeScript Integration (MEDIUM)

  • typescript-infer-dont-specify - Let TypeScript infer, type the queryFn
  • typescript-zod-validation - Use Zod for runtime validation

6. Testing Patterns (MEDIUM)

  • testing-fresh-client - Create fresh QueryClient per test
  • testing-msw-mocking - Use MSW for network mocking

7. Common Pitfalls (MEDIUM)

  • troubleshoot-copy-to-state - Never copy query data to local state
  • troubleshoot-missing-key-deps - Include all dependencies in query key
  • troubleshoot-fetch-not-reject - Handle HTTP errors with fetch

8. Migration to v5 (HIGH)

  • migration-single-signature - All hooks now take a single object argument
  • migration-status-pending - status 'loading' → 'pending'; isLoading semantics changed
  • migration-cache-time-to-gc-time - cacheTime renamed to gcTime
  • migration-keep-previous-data - keepPreviousData → placeholderData with identity fn
  • migration-query-callbacks-removed - onSuccess/onError/onSettled removed from useQuery
  • migration-suspense-hooks - New useSuspenseQuery, useSuspenseInfiniteQuery hooks
  • migration-throw-on-error - useErrorBoundary renamed to throwOnError
  • migration-remove-method - query.remove() removed; use queryClient.removeQueries()
  • migration-initial-page-param - initialPageParam now required for infinite queries
  • migration-refetch-page-to-max-pages - refetchPage replaced by maxPages
  • migration-hydration-boundary - Hydrate component renamed to HydrationBoundary
  • migration-refetch-interval-callback - refetchInterval callback now receives only query
  • migration-context-to-query-client - context prop removed; pass queryClient directly
  • migration-misc-breaking-changes - React 18 min, server retry=0, hashKey, isDataEqual, etc.

Core Mental Model

  1. React Query is NOT a data fetching library - it's an async state manager
  2. Server state!= Client state - never mix them in global state managers
  3. Stale-while-revalidate - show cached data immediately, fetch in background
  4. Query keys are dependencies - include all variables used in queryFn

How to Use

Read individual rule files for detailed explanations and code examples:

rules/query-key-factory.md
rules/mutation-invalidation.md
rules/websocket-event-invalidation.md

Each rule file contains:

  • Brief explanation of why it matters
  • Incorrect code example
  • Correct code example

Full Compiled Document

For the complete guide with all rules expanded: AGENTS.md

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

33.09%
按下载量换算191

Claude

28.36%
按下载量换算164

Cursor

19.02%
按下载量换算110

Gemini CLI

9.59%
按下载量换算55

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills