Skip to content

Bhavanapatil2911/-react-query-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mastering React Query: Project Documentation

This project was built step-by-step to learn TanStack React Query through a practical, real-world example (A Blog Posts Manager). This document serves as your cheat sheet and guide for the future.


🧐 What is React Query?

React Query (now officially TanStack Query) is a powerful data-fetching and state management library for React. It is often described as the "missing data-fetching library for React". It handles fetching, caching, synchronizing, and updating server state in your React applications.

🤷‍♂️ Why do we need it?

Before React Query, fetching data usually meant writing tedious boilerplate code:

  1. Creating useState for data.
  2. Creating useState for isLoading.
  3. Creating useState for error.
  4. Writing a useEffect to trigger the fetch on mount.
  5. Manually handling race conditions and component unmounting.

React Query replaces all of that with a single hook: useQuery. It automatically caches your data, deduplicates requests (if two components ask for the same data, it only makes one network call), and updates the UI in the background.

🎯 Where do we need it? (Use Cases)

You should use React Query whenever your application needs to talk to a server or external API.

  • Dashboards: Fetching metrics and background-polling for live updates.
  • E-commerce: Fetching product lists, managing shopping carts (mutations).
  • Social Media: Infinite scrolling feeds, optimistic "Likes".
  • Forms: Submitting data to the server and updating the local cache immediately.

📂 Project Walkthrough (Step-by-Step)

To review what you learned, open these files in order. Each file introduces a core concept of React Query.

1. src/main.jsx (The Setup)

Concept: QueryClientProvider and ReactQueryDevtools

  • What it teaches: How to initialize the React Query engine (QueryClient) and wrap your app so that every component has access to the cache. It also shows how to add the DevTools for debugging.

2. src/Posts.jsx (The Basics)

Concept: useQuery

  • What it teaches: How to fetch data from an API.
  • Key Takeaways:
    • queryKey: ['posts']: The unique name for this piece of data in the cache.
    • queryFn: The actual function that does the fetching.
    • Using the destructured isLoading and isError boolean variables to cleanly render loading spinners or error messages without useState.

3. src/CreatePost.jsx (Mutations & Cache Updates)

Concept: useMutation, useQueryClient, Cache Invalidation, and Optimistic Updates.

  • What it teaches: How to send data to the server (like submitting a form).
  • Key Takeaways:
    • useMutation doesn't run automatically; you have to call mutate() when the user submits the form.
    • The Cache Problem: When you add a new post, the old list is out of date.
    • The Solution: We used queryClient.setQueryData to instantly shove the new post into the top of our ['posts'] cache so the UI updates immediately without a page reload. (In a real app, you would also use queryClient.invalidateQueries to force a background refetch).

4. src/Comments.jsx (Advanced Queries)

Concept: Query Parameters and Dependent Queries.

  • What it teaches: How to fetch data that depends on something else (like fetching comments only after we know which Post ID the user clicked on).
  • Key Takeaways:
    • Dynamic Keys: queryKey: ['comments', postId]. This ensures we have a separate cache for every single post's comments.
    • Dependent Fetching: enabled: !!postId. This tells React Query: "Do not run this network request if the postId is null or undefined." It waits until the user actually clicks a post.

🚀 Next Steps

You have now mastered the 80% core of React Query! The next step is to explore the 5 advanced features we discussed: Infinite Scrolling, Prefetching, Polling, Optimistic Updates, and Parallel Queries.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors