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.
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.
Before React Query, fetching data usually meant writing tedious boilerplate code:
- Creating
useStatefordata. - Creating
useStateforisLoading. - Creating
useStateforerror. - Writing a
useEffectto trigger the fetch on mount. - 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.
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.
To review what you learned, open these files in order. Each file introduces a core concept of React Query.
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.
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
isLoadingandisErrorboolean variables to cleanly render loading spinners or error messages withoutuseState.
Concept: useMutation, useQueryClient, Cache Invalidation, and Optimistic Updates.
- What it teaches: How to send data to the server (like submitting a form).
- Key Takeaways:
useMutationdoesn't run automatically; you have to callmutate()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.setQueryDatato 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 usequeryClient.invalidateQueriesto force a background refetch).
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.
- Dynamic Keys:
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.