Cursor TanStack 编码规范

Model: qwen-max | ¥0.15/call
AI工具GPT-4.1智能助手CursorTanStack

Cursor TanStack 编码规范:来自 PatrickJS/awesome-cursorrules (40k stars) 的 tan,适用于各类文档与内容的智能化处理。

Calls: 1

Skill Documentation

Cursor TanStack 编码规范

摘要

Cursor TanStack 编码规范:来自 PatrickJS/awesome-cursorrules (40k stars) 的 tan,适用于各类文档与内容的智能化处理。

**文件来源:** PatrickJS/awesome-cursorrules → `rules/tanstack-query-v5-cursorrules-prompt-file.mdc`

**原仓库:** https://github.com/PatrickJS/awesome-cursorrules

**评分:** ⭐ 仓库 40k stars (社区最权威 Cursor rules 合集)

这个 skill 是干什么的?

把 `tanstack-query-v5-cursorrules-prompt-file.mdc` 这条 Cursor 编码规则打包成可调用的 AI skill,帮你把代码生成统一到一致的标准上。

> Cursor rules for TanStack Query v5 with query options, query key factories, mutations, optimistic updates, infinite queries, Suspense, and prefetching.

🤖 Agent 使用说明

👤 用户需要做什么?

适用场景

原始规则内容

globs: **/*
alwaysApply: false
---
You are an expert in TanStack Query v5 (formerly React Query), TypeScript, and async state management for React applications.

# TanStack Query v5 Guidelines

## Core Philosophy
- TanStack Query manages server state — it is NOT a general state manager for client-only state
- Every query should have a stable, serializable query key that uniquely describes the data
- Mutations handle writes; queries handle reads — never blur this boundary
- Prefer `queryOptions()` helper for reusable, co-located query definitions
- v5 breaking changes: `useQuery` no longer accepts positional args; always use the options object form

## Setup

// main.tsx

import { QueryClient, QueryClientProvider } from '@tanstack/react-query'

import { ReactQueryDevtools } from '@tanstack/react-query-devtools'

const queryClient = new QueryClient({

defaultOptions: {

queries: {

staleTime: 1000 * 60, // 1 minute default stale time

retry: 2,

refetchOnWindowFocus: true,

},

},

})

function App() {

return (

<QueryClientProvider client={queryClient}>

<YourApp />

<ReactQueryDevtools initialIsOpen={false} />

</QueryClientProvider>

)

}


## Query Keys
- Always structure keys as arrays: `['entity', 'list']`, `['entity', 'detail', id]`
- Use a query key factory to avoid typos and enable easy invalidation

// queryKeys.ts

export const postKeys = {

all: ['posts'] as const,

lists: () => [...postKeys.all, 'list'] as const,

list: (filters: PostFilters) => [...postKeys.lists(), filters] as const,

details: () => [...postKeys.all, 'detail'] as const,

detail: (id: string) => [...postKeys.details(), id] as const,

}


## queryOptions Helper (v5)
- Use `queryOptions()` to define queries once and reuse across components and loaders

import { queryOptions } from '@tanstack/react-query'

export const postQueryOptions = (id: string) =>

queryOptions({

queryKey: postKeys.detail(id),

queryFn: () => fetchPost(id),

staleTime: 1000 * 60 * 5, // 5 min

})

// In component

const { data } = useQuery(postQueryOptions(postId))

// In router loader (TanStack Router integration)

loader: ({ params, context: { queryClient } }) =>

queryClient.ensureQueryData(postQueryOptions(params.postId))


## useQuery

const {

data,

isLoading, // true only on first load with no cached data

isFetching, // true whenever a fetch is in-flight

isError,

error,

isSuccess,

} = useQuery({

queryKey: postKeys.detail(postId),

queryFn: () => fetchPost(postId),

enabled: !!postId, // disable query if params not ready

})


## useMutation

const { mutate, mutateAsync, isPending } = useMutation({

mutationFn: (newPost: CreatePostInput) => createPost(newPost),

onSuccess: (data) => {

// Invalidate and refetch

queryClient.invalidateQueries({ queryKey: postKeys.lists() })

toast.success('Post created!')

},

onError: (error) => {

toast.error(error.message)

},

})

...(完整内容在原仓库)...


## 数据来源

- 仓库: PatrickJS/awesome-cursorrules (40,581 stars, 257 个 .mdc 规则)
- 抓取时间: 2026-08-17
- License: 跟随原仓库(MIT)

## 联系方式

有问题或建议,在本 skill 下留言。

FAQ

这个 skill 是干什么的?

把 `tanstack-query-v5-cursorrules-prompt-file.mdc` 这条 Cursor 编码规则打包成可调用的 AI skill,帮你把代码生成统一到一致的标准上。

> Cursor rules for TanStack Query v5 with query options, query key factories, mutations, optimistic updates, infinite queries, Suspense, and prefetching.

👤 用户需要做什么?
  • [ ] 知道这条规则适合用在什么场景(参考下面"适用场景")
  • [ ] 把规则原文内容应用到 IDE 项目的 `.cursor/rules/` 目录(直接复制 .mdc 文件)
  • [ ] 调本 skill 时说清楚你的代码任务(语言/框架/目标)
  • [ ] 输出后人工 review 风格是否符合预期