tanstack-query-expert

Implements TanStack Query data fetching, mutations, and SSR hydration patterns in React and Next.js.

1|Updated Aug 17, 2025
One-click install
npx skills add https://github.com/ratnesh-maurya/mdconverter --skill tanstack-query-expert-ratnesh-maurya
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tanstack-query-expert
Source: https://github.com/ratnesh-maurya/mdconverter/tree/main/.claude/skills/tanstack-query-expert
Command: npx skills add https://github.com/ratnesh-maurya/mdconverter --skill tanstack-query-expert-ratnesh-maurya

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @tanstack/react-query.

What problem does it solve? Managing asynchronous server state in React with raw useEffect and useState leads to duplicated requests, stale caches, and brittle loading logic. This Skill provides production-grade TanStack Query patterns for declarative data fetching, cache invalidation, and Next.js App Router integration. ## Core Features & Use Cases - Query Patterns: Custom hook abstractions, strictly typed array-based query keys, and query key factories for large applications. - Mutations & Optimistic Updates: useMutation implementations with cache invalidation, optimistic UI updates, and rollback on failure. - Next.js SSR Integration: QueryClient provider setup, server-side prefetching, and HydrationBoundary-based dehydration for App Router. - Use Case: When refactoring a Next.js page that fetches posts via useEffect, use this Skill to convert it into a prefetched server component with a hydrated client-side useQuery hook and proper staleTime configuration. ## Quick Start Refactor my Next.js page that fetches user data with useEffect into a TanStack Query custom hook with server-side prefetching and hydration.

Frequently Asked Questions about tanstack-query-expert

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fetch data with TanStack Query in React?

Wrap your fetch logic in a custom hook calling useQuery with an array-based queryKey and a queryFn that returns a promise. Set staleTime to control background refetching and use the enabled option for dependent queries.

How to use TanStack Query with Next.js App Router?

Create a client-side Providers component with QueryClientProvider, then prefetch data in server components using queryClient.prefetchQuery. Pass the dehydrated state through HydrationBoundary so client components read cached data without refetching.

What is the difference between staleTime and gcTime in TanStack Query?

staleTime controls how long data is considered fresh before a background refetch triggers, while gcTime controls how long inactive data stays in memory after components unmount. If gcTime is shorter than staleTime, data is deleted before it becomes stale.

How do I implement optimistic updates with useMutation?

In onMutate, cancel outgoing queries, snapshot the previous cache with getQueryData, and update it with setQueryData. On error, roll back using the snapshot from context, and always invalidate queries in onSettled to resync with the server.

Why does TanStack Query keep refetching my data?

The default staleTime is 0, so queries refetch on every component remount and window focus. Set a global staleTime in QueryClient defaultOptions and disable refetchOnWindowFocus if your data does not change frequently.

Should I use useEffect or TanStack Query for data fetching?

Use TanStack Query instead of useEffect for server data fetching. It handles caching, request deduplication, background updates, and loading or error states automatically, eliminating manual synchronization logic.