Audit React Query usage for best practices — key factories, staleTime, mutations, and server state ownership
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-react-query-best-practices-c66bdc0dc150 ,按照其中的说明把「react-query-best-practices」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
Arguments:
User arguments: $ARGUMENTS
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.
Read these before analyzing:
Enforce .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:
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)enabled to prevent queries from running without required paramsqueryClient.prefetchQuery and shared queryOptions; never temporarily enable a mounted hidden observer, which can remain active after focus restoration and refetch data for closed UIenabled 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.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.queryFn so an optional warm cannot fail the page, and never bypass a route that redacts fields.Enforce .claude/rules/sim-queries.md "Mutation Hook" (targeted invalidation, onMutate/onError rollback, mutation objects out of useCallback deps). Additionally:
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"setQueryData is for optimistic updates and the server-prefetch seeding case in .claude/rules/sim-queries.md "Server prefetching", nothing else/you-might-not-need-an-effect "Query-backed forms"; do not duplicate its finding