tanstack-query-guide

Configures TanStack Query for React server-state management with useQuery and useMutation.

3|Updated Apr 5, 2024
One-click install
npx skills add https://github.com/danghungtb26/admin-react-typescript-template --skill tanstack-query-guide
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tanstack-query-guide
Source: https://github.com/danghungtb26/admin-react-typescript-template/tree/main/.agents/skills/tanstack-query-guide
Command: npx skills add https://github.com/danghungtb26/admin-react-typescript-template --skill tanstack-query-guide

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

TanStack Query simplifies server-state management in React apps by providing a robust caching layer, automatic background updates, and reliable data synchronization between the UI and backend.

Core Features & Use Cases

  • Data fetching with useQuery and built-in caching strategies to reduce unnecessary network requests.
  • Mutations with useMutation and optimistic updates to provide instant UI feedback.
  • Cache invalidation, prefetching, and structured query keys to keep data consistent across components.
  • Real-world scenario: a dashboard that displays live data, handles offline scenarios, and gracefully recovers after network errors.

Quick Start

Install @tanstack/react-query, configure a QueryClient, wrap your app with QueryClientProvider, and begin using useQuery and useMutation in components.

Frequently Asked Questions about tanstack-query-guide

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

FAQPage Schema
How do I manage server-state and data fetching in React applications?

To manage server-state in React, TanStack Query provides a robust caching layer and automatic background updates. It uses useQuery to fetch data and synchronizes the UI with the backend to reduce unnecessary network requests.

How do I set up TanStack Query for data caching in my React project?

To set up TanStack Query, install @tanstack/react-query, configure a QueryClient, and wrap your application with QueryClientProvider. You can then use useQuery and useMutation hooks in your components.

What is the best way to handle optimistic updates with useMutation in React?

The best way to handle optimistic updates is using useMutation from TanStack Query. It allows you to provide instant UI feedback by applying mutations locally before the server confirms the change.

How does cache invalidation and prefetching work with React Query?

Cache invalidation and prefetching in TanStack Query rely on structured query keys. This approach keeps data consistent across components by automatically synchronizing cached server-state with backend updates.

Does TanStack Query support offline scenarios and network error recovery?

Yes, TanStack Query handles offline scenarios and gracefully recovers after network errors. It manages server-state by automatically synchronizing data and providing reliable updates when connectivity is restored.

When do I need TanStack Query instead of traditional React state for server data?

You need TanStack Query when fetching server-state requires robust caching, automatic background updates, and query invalidation. It replaces manual React state management for synchronizing UI data with your backend.