tanstack-query_skill

This skill helps you implement TanStack Query best practices for data fetching, caching, mutations, and SSR in React applications.

68

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill deckardger/tanstack-agent-skills --skill tanstack-query

  • SKILL.md4.8 KB

Overview

This skill captures battle-tested best practices for using TanStack Query (React Query) in data-driven React applications. It focuses on robust query key design, caching, mutations, SSR hydration, and performance patterns to reduce bugs and improve UX. Use it to standardize server-state logic and make data flows predictable across your app.

How this skill works

The skill inspects common patterns and anti-patterns and presents concise rules grouped by priority: Query Keys, Caching, Mutations, Error Handling, Prefetching, SSR, and performance. Each rule includes an explanation, a bad example, a good example, and context for when to apply or skip it. Apply these rules while authoring or refactoring data fetching code to enforce consistency and prevent cache-related bugs.

When to use it

  • When designing new data fetching and caching layers for React apps
  • While implementing or refactoring mutations and optimistic updates
  • During SSR/SSG integration and hydration work
  • When configuring global QueryClient defaults and caching strategies
  • When diagnosing stale data, cache inconsistencies, or slow UX

Best practices

  • Always build query keys as arrays and include every dependent variable to avoid cache collisions
  • Set sensible staleTime and gcTime at QueryClient level; prefer targeted invalidation over broad purges
  • Invalidate related queries and provide rollback context in onMutate for safe optimistic updates
  • Use dehydrate/hydrate and a per-request QueryClient for SSR; increase server staleTime and wrap with HydrationBoundary
  • Leverage select, structural sharing, and notifyOnChangeProps to minimize re-renders and transform data cheaply

Example use cases

  • Creating a list/detail UI with hierarchical query keys: ['users', userId, { include }]
  • Implementing optimistic update for a create/edit mutation with rollback on error
  • Prefetching route data on hover and ensuring staleTime is set for preloaded queries
  • Hydrating server-fetched queries during SSG/SSR to eliminate loading flashes
  • Using useQueries for dynamic parallel fetches and cancelling unused requests

FAQ

Base staleTime on data volatility and UX needs: longer for rarely changing data, shorter for real-time feeds. Use server-side longer staleTime during SSR to reduce refetches on hydration.

When should I use optimistic updates?

Use optimistic updates for actions where immediate UI feedback matters (e.g., likes, counters). Always provide rollback context and robust error handling to restore consistent state on failure.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational