TanStack Query — Complete Guide
TanStack Query — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.
On this page
React.js Tutorial · Lesson 45 of 100
TanStack Query
Beginner ✓ → Intermediate → Advanced → Professional
Intermediate · 2 — Build apps · ~14 min read · Module 5: State & Authentication
Introduction
You know the basics now. Here we use TanStack Query in real app situations — forms, pages, and data. Still plain language, just a bit more depth. TanStack Query manages server state — fetching, caching, refetching, and loading/error flags. useQuery for reads, useMutation for writes. You stop writing the same useEffect + useState + loading pattern in every component.
State mistakes cause the most React bugs for beginners. Keep server data and UI state separate — this lesson shows how teams do that.
When will you use this?
Use these patterns when data is shared across pages or comes from a server.
- Shopping carts, user sessions, and API data need clear state patterns.
- Teams pick Context, Zustand, or Redux based on app size — you will see all three in jobs.
Real-world: cached product catalog
Flipkart category page caches product list — instant back navigation shows stale data while background refetch updates prices.
Production-style code
function CategoryPage({ categoryId }) {
const { data, isLoading, error } = useQuery({
queryKey: ['products', categoryId],
queryFn: () => fetch(`/api/categories/${categoryId}/products`).then(r => r.json()),
staleTime: 60_000
});
if (isLoading) return <GridSkeleton />;
if (error) return <ErrorState />;
return <ProductGrid products={data} />;
}
What happens in production: Query eliminates hand-rolled loading/error/cache logic — industry standard for server state in React since 2023.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
const { data, isLoading, error } = useQuery({
queryKey: ['products'],
queryFn: () => fetch('/api/products').then(r => r.json())
});
if (isLoading) return <p>Loading...</p>;
if (error) return <p>Error</p>;
return <ProductList items={data} />;
Line-by-line walkthrough
| Code | What it means |
|---|---|
const { data, isLoading, error } = useQuery({ | TanStack Query hook — loads or updates server data with caching built in. |
queryKey: ['products'], | Part of the TanStack Query example — read it together with the lines before and after. |
queryFn: () => fetch('/api/products').then(r => r.json()) | Defines a function — often a component or event handler. |
}); | Closes a block started by { or ( above. |
if (isLoading) return <p>Loading...</p>; | Sends UI or a value back to whoever called this function. |
if (error) return <p>Error</p>; | Sends UI or a value back to whoever called this function. |
return <ProductList items={data} />; | Sends UI or a value back to whoever called this function. |
How it works (big picture)
- queryKey identifies cache.
- Same key = cached data.
- isLoading and error are built in.
- Stale data refetches in background.
Do this on your computer
- npm install @tanstack/react-query
- Add QueryClientProvider in main.jsx.
- Replace one useEffect fetch with useQuery.
- Read the real-world section and name which part of the app uses this topic.
- Run the example locally and confirm the same behavior in the browser.
- Change one value in the example (text, initial state, or URL) and predict what will happen before you save.
Experiments — try changing this
- Change text or labels in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error message, then fix it.
- Open React DevTools (browser extension) while running TanStack Query and inspect component props/state.
Remember
TanStack Query = server state + cache. useQuery for GET-style data. Stable queryKey strings or arrays.
Common questions
Query vs Zustand?
Query for server/API data; Zustand for client-only UI state.
How long should I spend on TanStack Query?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.
What if I get stuck on TanStack Query?
Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.
Where is TanStack Query used in real jobs?
See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!