🗒 Cheat Sheet← Lesson
BitWithBite
React Worksheet

🗘️ React Query

Chapter: Real-World Projects & Deployment · Level ★★★ · Time: 20 min
SCORE___ / 20
NameClassDate
After this worksheet you can
Use useQuery to fetch dataUse useMutation for writesUse invalidateQueriesExplain staleTime
📚 Quick Recap

🎯 What you'll practice: React Query's useQuery and useMutation hooks.

🧠 Section A · Concept Check ● BEGINNER 4 × 1 = 4

1useQuery automatically handles:
2Create/update/delete operations use:
3invalidateQueries is called to:
4staleTime controls:

🧮 Section B · Problem Solving ● INTERMEDIATE 2 + 3×3 = 11

5useQuery takes a queryKey and a query.
6useMutation takes a mutation.
7Write a useQuery call fetching "/api/posts" with queryKey ["posts"].
8Write a useMutation call that deletes a post and invalidates ["posts"] on success.
9Why does React Query remove the need for manual isLoading/error state in every component?

🚀 Section C · Challenge ● CHALLENGE 5

10Design a Courses component using useQuery to list courses and useMutation to enroll, showing a spinner while loading.
💭 Reflection — the most useful thing I learned:
A ___/4   B ___/11   C ___/5   Total ___/20 Teacher's Signature Parent's Signature
✂ answer key — fold or cut before handing out

1-B   2-B   3-B   4-A  |  5 = Fn   6 = Fn   7 = const {data,isLoading}=useQuery({queryKey:["posts"],queryFn:()=>fetch("/api/posts").then(r=>r.json())});   8 = const del=useMutation({mutationFn:id=>fetch(`/api/posts/${id}`,{method:"DELETE"}),onSuccess:()=>queryClient.invalidateQueries(["posts"])});   9 = Because useQuery internally tracks and exposes isLoading/error/data itself — the same boilerplate that would otherwise be re-written with useState+useEffect in every fetching component is handled once, inside the library  |  10 = function Courses(){const {data,isLoading}=useQuery({queryKey:["courses"],queryFn:()=>fetch("/api/courses").then(r=>r.json())});const enroll=useMutation({mutationFn:id=>fetch(`/api/enroll/${id}`,{method:"POST"})});if(isLoading)return <Spinner/>;return data.map(c=><CourseCard key={c.id} {...c} onEnroll={()=>enroll.mutate(c.id)}/>);}

📄 Need offline practice?Print this worksheet or open the one-page cheat sheet.
🗒 Cheat Sheet