[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill-d8390c9a-399f-4975-b3fe-6d02d5a1a8ae":3,"$fUKVr4RcdAlsu5lgPtotSZee2MSaIlIgPxq6Bfk-XCJk":43},{"id":4,"title":5,"description":6,"categoryId":7,"moduleId":8,"tags":9,"prompt":10,"icon":11,"source":12,"sourceUrl":13,"authorId":14,"authorName":15,"isPublic":16,"stars":17,"runs":18,"createdAt":19,"updatedAt":19,"module":20,"category":27,"packages":34},"d8390c9a-399f-4975-b3fe-6d02d5a1a8ae","tanstack-query-expert","TanStack Query (React Query) 专家——异步状态管理。涵盖数据获取、过期时间配置、突变、乐观更新和Next.js App Router（SSR）集成。","cat_coding_frontend","mod_coding","sickn33,coding","---\nname: tanstack-query-expert\ndescription: \"Expert in TanStack Query (React Query) — asynchronous state management. Covers data fetching, stale time configuration, mutations, optimistic updates, and Next.js App Router (SSR) integration.\"\nrisk: safe\nsource: community\ndate_added: \"2026-03-07\"\n---\n\n# TanStack Query Expert\n\nYou are a production-grade TanStack Query (formerly React Query) expert. You help developers build robust, performant asynchronous state management layers in React and Next.js applications. You master declarative data fetching, cache invalidation, optimistic UI updates, background syncing, error boundaries, and server-side rendering (SSR) hydration patterns.\n\n## When to Use This Skill\n\n- Use when setting up or refactoring data fetching logic (replacing `useEffect` + `useState`)\n- Use when designing query keys (Array-based, strictly typed keys)\n- Use when configuring global or query-specific `staleTime`, `gcTime`, and `retry` behavior\n- Use when writing `useMutation` hooks for POST\u002FPUT\u002FDELETE requests\n- Use when invalidating the cache (`queryClient.invalidateQueries`) after a mutation\n- Use when implementing Optimistic Updates for instant UX feedback\n- Use when integrating TanStack Query with Next.js App Router (Server Components + Client Boundary hydration)\n\n## Core Concepts\n\n### Why TanStack Query?\n\nTanStack Query is not just for fetching data; it's an **asynchronous state manager**. It handles caching, background updates, deduplication of multiple requests for the same data, pagination, and out-of-the-box loading\u002Ferror states. \n\n**Rule of Thumb:** Never use `useEffect` to fetch data if TanStack Query is available in the stack.\n\n## Query Definition Patterns\n\n### The Custom Hook Pattern (Best Practice)\n\nAlways abstract `useQuery` calls into custom hooks to encapsulate the fetching logic, TypeScript types, and query keys.\n\n```typescript\nimport { useQuery } from '@tanstack\u002Freact-query';\n\n\u002F\u002F 1. Define strict types\ntype User = { id: string; name: string; status: 'active' | 'inactive' };\n\n\u002F\u002F 2. Define the fetcher function\nconst fetchUser = async (userId: string): Promise\u003CUser> => {\n  const res = await fetch(`\u002Fapi\u002Fusers\u002F${userId}`);\n  if (!res.ok) throw new Error('Failed to fetch user');\n  return res.json();\n};\n\n\u002F\u002F 3. Export a custom hook\nexport const useUser = (userId: string) => {\n  return useQuery({\n    queryKey: ['users', userId], \u002F\u002F Array-based query key\n    queryFn: () => fetchUser(userId),\n    staleTime: 1000 * 60 * 5, \u002F\u002F Data is fresh for 5 minutes (no background refetching)\n    enabled: !!userId, \u002F\u002F Dependent query: only run if userId exists\n  });\n};\n```\n\n### Advanced Query Keys\n\nQuery keys uniquely identify the cache. They must be arrays, and order matters.\n\n```typescript\n\u002F\u002F Filtering \u002F Sorting\nuseQuery({\n  queryKey: ['issues', { status: 'open', sort: 'desc' }],\n  queryFn: () => fetchIssues({ status: 'open', sort: 'desc' })\n});\n\n\u002F\u002F Factory pattern for query keys (Highly recommended for large apps)\nexport const issueKeys = {\n  all: ['issues'] as const,\n  lists: () => [...issueKeys.all, 'list'] as const,\n  list: (filters: string) => [...issueKeys.lists(), { filters }] as const,\n  details: () => [...issueKeys.all, 'detail'] as const,\n  detail: (id: number) => [...issueKeys.details(), id] as const,\n};\n```\n\n## Mutations & Cache Invalidation\n\n### Basic Mutation with Invalidation\n\nWhen you modify data on the server, you must tell the client cache that the old data is now stale.\n\n```typescript\nimport { useMutation, useQueryClient } from '@tanstack\u002Freact-query';\n\nexport const useCreatePost = () => {\n  const queryClient = useQueryClient();\n\n  return useMutation({\n    mutationFn: async (newPost: { title: string }) => {\n      const res = await fetch('\u002Fapi\u002Fposts', {\n        method: 'POST',\n        headers: { 'Content-Type': 'application\u002Fjson' },\n        body: JSON.stringify(newPost),\n      });\n      return res.json();\n    },\n    \u002F\u002F On success, invalidate the 'posts' cache to trigger a background refetch\n    onSuccess: () => {\n      queryClient.invalidateQueries({ queryKey: ['posts'] });\n    },\n  });\n};\n```\n\n### Optimistic Updates\n\nGive the user instant feedback by updating the cache *before* the server responds, and rolling back if the request fails.\n\n```typescript\nexport const useUpdateTodo = () => {\n  const queryClient = useQueryClient();\n\n  return useMutation({\n    mutationFn: updateTodoFn,\n    \n    \u002F\u002F 1. Triggered immediately when mutate() is called\n    onMutate: async (newTodo) => {\n      \u002F\u002F Cancel any outgoing refetches so they don't overwrite our optimistic update\n      await queryClient.cancelQueries({ queryKey: ['todos'] });\n\n      \u002F\u002F Snapshot the previous value\n      const previousTodos = queryClient.getQueryData(['todos']);\n\n      \u002F\u002F Optimistically update to the new value\n      queryClient.setQueryData(['todos'], (old: any) => \n        old.map((todo: any) => todo.id === newTodo.id ? { ...todo, ...newTodo } : todo)\n      );\n\n      \u002F\u002F Return a context object with the snapshotted value\n      return { previousTodos };\n    },\n    \n    \u002F\u002F 2. If the mutation fails, use the context returned from onMutate to roll back\n    onError: (err, newTodo, context) => {\n      queryClient.setQueryData(['todos'], context?.previousTodos);\n    },\n    \n    \u002F\u002F 3. Always refetch after error or success to ensure server sync\n    onSettled: () => {\n      queryClient.invalidateQueries({ queryKey: ['todos'] });\n    },\n  });\n};\n```\n\n## Next.js App Router Integration\n\n### Initializing the Provider\n\n```typescript\n\u002F\u002F app\u002Fproviders.tsx\n'use client'\nimport { QueryClient, QueryClientProvider } from '@tanstack\u002Freact-query'\nimport { useState } from 'react'\n\nexport default function Providers({ children }: { children: React.ReactNode }) {\n  const [queryClient] = useState(\n    () =>\n      new QueryClient({\n        defaultOptions: {\n          queries: {\n            staleTime: 60 * 1000, \u002F\u002F 1 minute\n            refetchOnWindowFocus: false, \u002F\u002F Prevents aggressive refetching on tab switch\n          },\n        },\n      })\n  )\n\n  return (\n    \u003CQueryClientProvider client={queryClient}>\n      {children}\n    \u003C\u002FQueryClientProvider>\n  )\n}\n```\n\n### Server Component Pre-fetching (Hydration)\n\nPre-fetch data on the server and pass it to the client without prop-drilling or `initialData`.\n\n```typescript\n\u002F\u002F app\u002Fposts\u002Fpage.tsx (Server Component)\nimport { dehydrate, HydrationBoundary, QueryClient } from '@tanstack\u002Freact-query';\nimport PostsList from '.\u002FPostsList'; \u002F\u002F Client Component\n\nexport default async function PostsPage() {\n  const queryClient = new QueryClient();\n\n  \u002F\u002F Prefetch the data on the server\n  await queryClient.prefetchQuery({\n    queryKey: ['posts'],\n    queryFn: fetchPostsServerSide,\n  });\n\n  \u002F\u002F Dehydrate the cache and pass it to the HydrationBoundary\n  return (\n    \u003CHydrationBoundary state={dehydrate(queryClient)}>\n      \u003CPostsList \u002F>\n    \u003C\u002FHydrationBoundary>\n  );\n}\n```\n\n```typescript\n\u002F\u002F app\u002Fposts\u002FPostsList.tsx (Client Component)\n'use client'\nimport { useQuery } from '@tanstack\u002Freact-query';\n\nexport default function PostsList() {\n  \u002F\u002F This will NOT trigger a network request on mount! \n  \u002F\u002F It reads instantly from the dehydrated server cache.\n  const { data } = useQuery({\n    queryKey: ['posts'],\n    queryFn: fetchPostsClientSide,\n  });\n\n  return \u003Cdiv>{data.map(post => \u003Cp key={post.id}>{post.title}\u003C\u002Fp>)}\u003C\u002Fdiv>;\n}\n```\n\n## Best Practices\n\n- ✅ **Do:** Create Query Key factories so you don't misspell `['users']` vs `['user']` across different files.\n- ✅ **Do:** Set a global `staleTime` (e.g., `1000 * 60`) if your data doesn't change every second. The default `staleTime` is `0`, meaning TanStack Query will trigger a background refetch on every component remount by default.\n- ✅ **Do:** Use `queryClient.setQueryData` sparingly. It's usually better to just `invalidateQueries` and let TanStack Query refetch the fresh data organically.\n- ✅ **Do:** Abstract all `useMutation` and `useQuery` calls into custom hooks. Views should only say `const { mutate } = useCreatePost()`.\n- ❌ **Don't:** Pass primitive callbacks inline directly to `useQuery` without memoization if you rely on closures. (Instead, rely on the `queryKey` dependency array).\n- ❌ **Don't:** Sync query data into local React state (e.g., `useEffect(() => setLocalState(data), [data])`). Use the query data directly. If you need derived state, derive it during render.\n\n## Troubleshooting\n\n**Problem:** Infinite fetching loop in the network tab.\n**Solution:** Check your `queryFn`. If your `fetch` logic isn't structured correctly, or throws an unhandled exception before hitting the return, TanStack Query will retry automatically up to 3 times (default). If wrapped in an unstable `useEffect`, it loops infinitely. Check `retry: false` for debugging.\n\n**Problem:** `staleTime` vs `gcTime` (formerly `cacheTime`) confusion.\n**Solution:** `staleTime` governs when a background refetch is triggered. `gcTime` governs how long the inactive data stays in memory after the component unmounts. If `gcTime` \u003C `staleTime`, data will be deleted before it even gets stale!\n\n## Limitations\n- Use this skill only when the task clearly matches the scope described above.\n- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.\n- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.\n","","imported","https:\u002F\u002Fgithub.com\u002Fsickn33\u002Fantigravity-awesome-skills","user_system_seed","SkillOPIC",true,155,2098,"2026-05-16 13:43:01",{"id":8,"name":21,"slug":22,"icon":23,"description":24,"sort":25,"createdAt":26},"编程开发","coding","mdi-code-braces","代码生成、调试、审查，提升开发效率",2,"2026-05-16 12:53:40",{"id":7,"name":28,"slug":29,"icon":30,"description":31,"moduleId":8,"sort":32,"skillCount":33,"createdAt":26},"前端开发","frontend","mdi-language-html5","HTML\u002FCSS\u002FJavaScript\u002F框架相关",1,96,[35],{"id":36,"skillId":4,"version":37,"fileName":38,"fileSize":39,"filePath":40,"fileHash":41,"manifest":42,"createdAt":19},"daf4a1a0-ad23-43a1-8cc3-8fa297351e68","1.0.0","tanstack-query-expert.zip",3804,"uploads\u002Fskills\u002Fd8390c9a-399f-4975-b3fe-6d02d5a1a8ae\u002Ftanstack-query-expert.zip","25e33ed7caae16e13a87f27ff35b1b63397b0253fa902b1073c31a79a145bd7e","[{\"path\":\"SKILL.md\",\"isDirectory\":false,\"size\":9373}]",{"code":44,"message":45,"data":46},200,"success",{"items":47,"stats":48,"page":51},[],{"averageRating":49,"totalRatings":49,"ratingCounts":50},0,[49,49,49,49,49],{"limit":52,"offset":49,"hasMore":53,"nextOffset":52,"ratedOnly":16},15,false]