मुख्य सामग्री पर जाएँ
Skillpedia
वेबसाइट और UI (frontend)7.2k · #13312

tanstack-query

यह क्या करती है

यह स्किल एजेंट को TanStack Query वर्शन 5 से सर्वर डेटा लाने और कैश करने का मार्गदर्शन देती है। यह उन डेवलपर्स के लिए है जो React ऐप बनाते हैं जो किसी ऑनलाइन सेवा से डेटा लेते हैं।

कब इस्तेमाल करें

  • ✓जब किसी React कॉम्पोनेंट को API से डेटा लाना हो।
  • ✓जब आप चाहते हैं कि लोडिंग और कैशिंग एक एकसमान तरीके से संभाली जाएँ।
  • ✓जब कोई कार्रवाई डेटा बदलती है और स्क्रीन को उससे मेल खाने के लिए ताज़ा होना चाहिए।

Skill का लिंक

https://github.com/Kiranism/next-shadcn-dashboard-starter/blob/main/.agents/skills/tanstack-query/SKILL.md

इसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।

कैसे इस्तेमाल करें

सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।

पेस्ट करने के लिए तैयार मैसेज

इस skill से मेरी मदद करो: https://github.com/Kiranism/next-shadcn-dashboard-starter/blob/main/.agents/skills/tanstack-query/SKILL.md इस लिंक की SKILL.md फ़ाइल पढ़ो और मेरे काम में उसके निर्देशों का पालन करो।
क्या आप चाहते हैं कि आपका AI ऐसी skills खुद ढूँढे?मुफ़्त Skillpedia MCP इंस्टॉल करें: बस एक लाइन, और हर काम से पहले यह 15,000+ skills में खोजता है।1 मिनट में इंस्टॉल करें →

AI से क्या माँगें: उदाहरण

“मेरे React ऐप में ऑर्डर की सूची लाइए, और नया ऑर्डर जुड़ने के बाद उसे ताज़ा कीजिए।”

हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।

यह कैसे काम करती है

एजेंट query hooks और एक केंद्रीय API सर्विस फ़ाइल इस्तेमाल करता है, फिर बदलाव के बाद कैश किया डेटा ताज़ा या अपडेट करता है। वह वर्शन 5 के नामों और नियमों का पालन करता है।

तकनीकी सामग्री (मूल SKILL.md)

लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।

TanStack Query Patterns

Purpose

Modern data fetching with TanStack Query v5 (latest: 5.90.5, November 2025), emphasizing Suspense-based queries, cache-first strategies, and centralized API services.

Note: v5 (released October 2023) has breaking changes from v4:

  • isLoading → isPending for status
  • cacheTime → gcTime (garbage collection time)
  • React 18.0+ required
  • Callbacks removed from useQuery (onError, onSuccess, onSettled)
  • keepPreviousData replaced with placeholderData function

When to Use This Skill

  • Fetching data with TanStack Query
  • Using useSuspenseQuery or useQuery
  • Managing mutations
  • Cache invalidation and updates
  • API service patterns

Quick Start

Primary Pattern: useSuspenseQuery

For all new components, use useSuspenseQuery:

import { useSuspenseQuery } from '@tanstack/react-query';
import { postsApi } from '~/features/posts/api/postsApi';

function PostList() {
  const { data: posts } = useSuspenseQuery({
    queryKey: ['posts'],
    queryFn: postsApi.getAll,
  });

  return (
    <div>
      {posts.map(post => (
        <PostCard key={post.id} post={post} />
      ))}
    </div>
  );
}

// Wrap with Suspense
<Suspense fallback={<PostsSkeleton />}>
  <PostList />
</Suspense>

Benefits:

  • No isLoading checks needed
  • Integrates with Suspense boundaries
  • Cleaner component code
  • Consistent loading UX

useSuspenseQuery Patterns

Basic Usage

const { data } = useSuspenseQuery({
  queryKey: ['user', userId],
  queryFn: () => userApi.get(userId),
});

// data is never undefined - guaranteed by Suspense
return <div>{data.name}</div>;

With Parameters

function UserPosts({ userId }: { userId: string }) {
  const { data: posts } = useSuspenseQuery({
    queryKey: ['users', userId, 'posts'],
    queryFn: () => postsApi.getByUser(userId),
  });

  return <div>{posts.length} posts</div>;
}

Dependent Queries

function PostDetails({ postId }: { postId: string }) {
  // First query
  const { data: post } = useSuspenseQuery({
    queryKey: ['posts', postId],
    queryFn: () => postsApi.get(postId),
  });

  // Second query depends on first
  const { data: author } = useSuspenseQuery({
    queryKey: ['users', post.authorId],
    queryFn: () => userApi.get(post.authorId),
  });

  return <div>{author.name} wrote {post.title}</div>;
}

useQuery (Legacy Pattern)

Use useQuery only when you need loading/error states in the component:

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

function Component() {
  const { data, isPending, error } = useQuery({
    queryKey: ['posts'],
    queryFn: postsApi.getAll,
  });

  if (isPending) return <Spinner />;
  if (error) return <Error error={error} />;

  return <div>{data.map(...)}</div>;
}

When to use useQuery vs useSuspenseQuery:

  • Use useSuspenseQuery by default (preferred)
  • Use useQuery only when you need component-level loading states
  • Most cases should use useSuspenseQuery + Suspense boundaries

Mutations

Basic Mutation

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

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

  const mutation = useMutation({
    mutationFn: postsApi.create,
    onSuccess: () => {
      // Invalidate and refetch
      queryClient.invalidateQueries({ queryKey: ['posts'] });
    },
  });

  const handleCreate = () => {
    mutation.mutate({
      title: 'New Post',
      content: 'Content here',
    });
  };

  return (
    <button onClick={handleCreate} disabled={mutation.isPending}>
      {mutation.isPending ? 'Creating...' : 'Create Post'}
    </button>
  );
}

Optimistic Updates

const mutation = useMutation({
  mutationFn: postsApi.update,
  onMutate: async (updatedPost) => {
    // Cancel outgoing refetches
    await queryClient.cancelQueries({ queryKey: ['posts', updatedPost.id] });

    // Snapshot previous value
    const previousPost = queryClient.getQueryData(['posts', updatedPost.id]);

    // Optimistically update
    queryClient.setQueryData(['posts', updatedPost.id], updatedPost);

    // Return context with snapshot
    return { previousPost };
  },
  onError: (err, updatedPost, context) => {
    // Rollback on error
    queryClient.setQueryData(['posts', updatedPost.id], context.previousPost);
  },
  onSettled: (data, error, variables) => {
  
अक्सर पूछे जाने वाले सवाल

वर्शन 5 में क्या बदला?

कुछ नाम बदले, जैसे isLoading का isPending बनना, और queries से कुछ callbacks हटाए गए।

suspense लोडिंग क्या है?

डेटा लोड होते समय यह एक फ़ॉलबैक दिखाता है, इसलिए कॉम्पोनेंट को अपनी अलग लोडिंग जाँच नहीं चाहिए।

API कॉल कहाँ रहनी चाहिए?

स्किल एक केंद्रीय API सर्विस फ़ाइल की सिफ़ारिश करती है, जिसे query hooks कॉल करते हैं।

जानकारी

लेखक का मूल विवरण

TanStack Query v5 data fetching patterns including useSuspenseQuery, useQuery, mutations, cache management, and API service integration. Use when fetching data, managing server state, or working with TanStack Query hooks.

repo: Kiranism/next-shadcn-dashboard-starter

लोकप्रियता

GitHub स्टार
7.2k
रैंकिंग में स्थान
#13312
लाइसेंस
MIT
अपडेट की गई
1/11/2026

क्या यह skill काम की है?

0

लॉग इन करें वोट करने के लिए।

कमेंट

  • …

इस repository की और skills

मिलती-जुलती skills