Agent Skillssimstudioai/sim › react-query-best-practices

react-query-best-practices

GitHub

审计 React Query 使用规范,确保键工厂、缓存策略及服务端状态管理的最佳实践,支持修复或仅提案更改。

.agents/skills/react-query-best-practices/SKILL.md simstudioai/sim

Trigger Scenarios

检查 React Query 代码实现 验证服务端状态管理规范性

Install

npx skills add simstudioai/sim --skill react-query-best-practices -g -y
More Options

Non-standard path

npx skills add https://github.com/simstudioai/sim/tree/main/.agents/skills/react-query-best-practices -g -y

Use without installing

npx skills use simstudioai/sim@react-query-best-practices

指定 Agent (Claude Code)

npx skills add simstudioai/sim --skill react-query-best-practices -a claude-code -g -y

安装 repo 全部 skill

npx skills add simstudioai/sim --all -g -y

预览 repo 内 skill

npx skills add simstudioai/sim --list

SKILL.md

Frontmatter
{
    "name": "react-query-best-practices",
    "description": "Audit React Query usage for best practices — key factories, staleTime, mutations, and server state ownership",
    "argument-hint": "[scope] [fix=true|false]"
}

React Query Best Practices

Arguments:

  • scope: what to analyze (default: your current changes). Examples: "diff to main", "PR #123", "src/hooks/queries/", "whole codebase"
  • fix: whether to apply fixes (default: true). Set to false to only propose changes.

User arguments: $ARGUMENTS

Context

This codebase uses React Query (TanStack Query) as the single source of truth for all server state. All query hooks live in hooks/queries/. Zustand is used only for client-only UI state. Server data must never be duplicated into useState or Zustand outside of mutation callbacks that coordinate cross-store state.

References

Read these before analyzing:

  1. https://tkdodo.eu/blog/practical-react-query — foundational defaults, custom hooks, avoiding local state copies
  2. https://tkdodo.eu/blog/effective-react-query-keys — key factory pattern, hierarchical keys, fuzzy invalidation
  3. https://tkdodo.eu/blog/react-query-as-a-state-manager — React Query IS your server state manager

Rules to enforce

Query keys and hooks

Enforce CLAUDE.md "React Query" and .claude/rules/sim-queries.md (key factory with all + plural prefixes, signal forwarding, named staleTime constants reused by prefetches, keepPreviousData only on variable keys, requestJson boundary). Additionally:

  • Key factories live next to their hooks — except a factory, standalone fetcher/mapper, or staleTime constant that a server module (a prefetch.ts, route, block, trigger) imports, which must live in a non-'use client' module under hooks/queries/utils/ per .claude/rules/sim-queries.md (a 'use client' export called from the server crashes SSR)
  • Use enabled to prevent queries from running without required params
  • Warm data for hover/focus intent with queryClient.prefetchQuery and shared queryOptions; never temporarily enable a mounted hidden observer, which can remain active after focus restoration and refetch data for closed UI
  • When gating a query by view or modal state, move every consumer to the active query too: imperative refresh/pagination, loading and error feedback, and data-derived controls must never read a disabled query or placeholder data from a previous key
  • Compose caller-controlled enabled options with required-param guards (Boolean(id) && (options?.enabled ?? true)). Never spread options after an internal guard, because { enabled: true } can silently re-enable an invalid request.
  • A disabled query can still report isPending: true. Aggregate loading state only for queries that are applicable/enabled, or an optional query can hold the whole surface in a permanent loading state.
  • Deferred authorization or policy queries must fail closed. Do not give pending/error data the same fallback as a successfully loaded unrestricted policy; disable guarded actions until the policy query succeeds.
  • Server prefetches must call the authorized use case, apply the route presenter/response schema, and reuse the client's exact key, mapper, and stale time. Keep all fallible auth/read/parse work inside queryFn so an optional warm cannot fail the page, and never bypass a route that redacts fields.

Mutations

Enforce CLAUDE.md "Mutation Hooks" (targeted invalidation, onMutate/onError rollback, mutation objects out of useCallback deps). Additionally:

  • Plain mutations invalidate in onSuccess; optimistic mutations reconcile in onSettled (fires on success and error) with rollback in onError — see .claude/rules/sim-queries.md "Mutation Hook" / "Optimistic Updates"

Server state ownership

  • Never copy query data into useState. Use query data directly in components.
  • Never copy query data into Zustand stores (exception: mutation callbacks that coordinate cross-store state like temp ID replacement)
  • The query cache is not a local state manager — setQueryData is for optimistic updates and the server-prefetch seeding case in .claude/rules/sim-queries.md "Server prefetching", nothing else
  • Forms are the one deliberate exception (a keyed form child initialized lazily from loaded query data) — the pattern is owned by /you-might-not-need-an-effect "Query-backed forms"; do not duplicate its finding

Steps

  1. Read the references above to understand the guidelines
  2. Analyze the specified scope against the rules listed above
  3. If fix=true, apply the fixes. If fix=false, propose the fixes without applying.

Version History

  • 6dfb467 Current 2026-09-23 06:36

    移除过时模式与冗余内容,修正提供者矩阵及仓库特定声明

  • 336ec07 2026-08-29 03:06

    新增 query hooks 规则:处理 enabled 选项组合、禁用查询的加载状态聚合、权限查询失败关闭及服务端预取逻辑。

  • ceda457 2026-08-20 15:29

Same Skill Collection

.agents/skills/add-block-preview/SKILL.md
.agents/skills/add-block/SKILL.md
.agents/skills/add-column-type/SKILL.md
.agents/skills/add-connector/SKILL.md
.agents/skills/add-enrichment/SKILL.md
.agents/skills/add-feature-flag/SKILL.md
.agents/skills/add-hosted-key/SKILL.md
.agents/skills/add-integration/SKILL.md
.agents/skills/add-managed-cli/SKILL.md
.agents/skills/add-model/SKILL.md
.agents/skills/add-permission-group-item/SKILL.md
.agents/skills/add-selector/SKILL.md
.agents/skills/add-tools/SKILL.md
.agents/skills/add-trigger/SKILL.md
.agents/skills/babysit/SKILL.md
.agents/skills/cleanup/SKILL.md
.agents/skills/council/SKILL.md
.agents/skills/db-migrate/SKILL.md
.agents/skills/design-taste-frontend/SKILL.md
.agents/skills/emcn-design-review/SKILL.md
.agents/skills/emil-design-eng/SKILL.md
.agents/skills/make-interfaces-feel-better/SKILL.md
.agents/skills/memory-load-check/SKILL.md
.agents/skills/ship/SKILL.md
.agents/skills/tool-registry-boundary/SKILL.md
.agents/skills/v2-api-conventions/SKILL.md
.agents/skills/validate-connector/SKILL.md
.agents/skills/validate-integration/SKILL.md
.agents/skills/validate-model/SKILL.md
.agents/skills/validate-permission-group-item/SKILL.md
.agents/skills/validate-selector/SKILL.md
.agents/skills/validate-trigger/SKILL.md
.agents/skills/you-might-not-need-a-callback/SKILL.md
.agents/skills/you-might-not-need-a-comment/SKILL.md
.agents/skills/you-might-not-need-a-memo/SKILL.md
.agents/skills/you-might-not-need-an-effect/SKILL.md
.agents/skills/you-might-not-need-state/SKILL.md
.agents/skills/you-might-not-need-url-state/SKILL.md
.claude/skills/add-settings-page/SKILL.md
helm/sim/.claude/skills/sim-helm/SKILL.md
.agents/skills/migrate-application-operation/SKILL.md

Metadata

Files
0
Version
6dfb467
Hash
2af9deb1
Indexed
2026-08-20 15:29

trang chủ - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-09-23 09:11
浙ICP备14020137号-1