Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

tanstack-query

БесплатноБез исполняемых скриптовНе проверен

TanStack Query for data fetching. Covers queries, mutations, and caching. Use for server state management in React. USE WHEN: user mentions "tanstack query"…

claude-dev-suiteот claude-dev-suite★ 51 звезда на GitHub

Об этом скилле

TanStack Query Core Knowledge

Deep Knowledge: Use mcp__documentation__fetch_docs with technology: tanstack-query for comprehensive documentation.

Setup

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

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 1000 * 60,  // 1 minute
      retry: 1,
    },
  },
});

function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <MyApp />
    </QueryClientProvider>
  );
}

Queries

import { useQuery } from '@tanstack/react-query';

function UserProfile({ userId }: { userId: string }) {
  const { data, isLoading, error } = useQuery({
    queryKey: ['user', userId],
    queryFn: () => fetchUser(userId),
  });

  if (isLoading) return <Spinner />;
  if (error) return <Error message={error.message} />;
  return <div>{data.name}</div>;
}

// With options
const { data } = useQuery({
  queryKey: ['users', { status: 'active' }],
  queryFn: () => fetchUsers({ status: 'active' }),
  staleTime: 5 * 60 * 1000,
  refetchOnWindowFocus: false,
  enabled: !!userId,  // Conditional fetch
});

Mutations

import { useMutation, useQueryClient } from '@tanstack/react-query';

function CreateUser() {
  const queryClient = useQueryClient();

  const mutation = useMutation({
    mutationFn: (newUser: CreateUserInput) => createUser(newUser),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['users'] });
    },
  });

  return (
    <form onSubmit={(e) => {
      e.preventDefault();
      mutation.mutate({ name: 'John' });
    }}>
      <button disabled={mutation.isPending}>
        {mutation.isPending ? 'Creating...' : 'Create'}
      </button>
    </form>
  );
}

Optimistic Updates

const mutation = useMutation({
  mutationFn: updateUser,
  onMutate: async (newData) => {
    await queryClient.cancelQueries({ queryKey: ['user', id] });
    const previous = queryClient.getQueryData(['user', id]);
    queryClient.setQueryData(['user', id], newData);
    return { previous };
  },
  onError: (err, newData, context) => {
    queryClient.setQueryData(['user', id], context.previous);
  },
  onSettled: () => {
    queryClient.invalidateQueries({ queryKey: ['user', id] });
  },
});

When NOT to Use This Skill

Scenario Use Instead
Client-side UI state (modals, form inputs) zustand or React state
Vue 3 applications pinia with custom composables
Static configuration data React Context API
Real-time WebSocket data Custom hooks with WebSocket + Zustand
GraphQL queries @apollo/client or urql

Anti-Patterns

Anti-Pattern Why It's Bad Correct Approach
Using queries for client state Unnecessary complexity, wrong abstraction Use Zustand or React state
Not using query keys properly Cache collisions, wrong data Use query key factory pattern
Fetching on every render Performance issues Set proper staleTime and gcTime
Manual cache invalidation everywhere Hard to maintain Use mutation's onSuccess with invalidateQueries
Ignoring loading/error states Poor UX Always handle isLoading and error
Using refetch() instead of invalidate() Bypasses cache, wastes requests Use invalidateQueries() for revalidation
Not prefetching predictable navigation Slow perceived performance Prefetch on hover/mount
Storing queries in global state Defeats TanStack Query purpose Let TanStack Query manage cache
No retry strategy for transient errors Failed requests on network blips Configure retry with backoff
Missing query key dependencies Stale data when params change Include all variables in query key

Quick Troubleshooting

Issue Cause Solution
Queries not refetching staleTime too high or enabled: false Lower staleTime or check enabled condition
"No QueryClient set" error Missing QueryClientProvider Wrap app with <QueryClientProvider client={queryClient}>
Mutations not updating UI Not invalidating queries Call queryClient.invalidateQueries() in onSuccess
Infinite refetch loop Query key changes on every render Stabilize query key with useMemo or constants
SSR hydration mismatch Server/client data out of sync Use HydrationBoundary with dehydrated state
Memory leaks Queries never garbage collected Set proper gcTime (default 5 minutes)
Optimistic update reverted incorrectly Context not returned from onMutate Return previous value from onMutate, restore in onError
Multiple identical requests No deduplication interval Set dedupingInterval in query options

Production Readiness

Query Client Configuration

// lib/queryClient.ts
import { QueryClient, QueryCache, MutationCache } from '@tanstack/react-q
Полный SKILL.md на GitHub

Установить tanstack-query в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Read
Grep
Glob
Write
Edit

Вложенные файлы

quick-ref/mutations.mdquick-ref/openapi-types.mdquick-ref/queries.md

FAQ

Что делает скилл tanstack-query?

TanStack Query for data fetching. Covers queries, mutations, and caching. Use for server state management in React. USE WHEN: user mentions "tanstack query", "react query", "data fetching", "API calls", asks about "cache management", "mutations", "infinite scroll", "optimistic updates", "prefetching", "server state", "useQuery", "useMutation" DO NOT USE FOR: client state - use `zustand` or `redux-toolkit`; Vue apps - use `pinia` with composables; static data - use React context

Как установить скилл tanstack-query?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл tanstack-query запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить tanstack-query с