What You'll Learn
You'll learn how TanStack Query intelligently prevents duplicate network requests when multiple components use the same query key simultaneously.
The Problem of Duplicate Requests
Imagine three components on the same page all need the same user profile data. Without TanStack Query, each component might trigger its own fetch request:
// Component A
useEffect(() => fetch('/api/user/123'), []);
// Component B
useEffect(() => fetch('/api/user/123'), []);
// Component C
useEffect(() => fetch('/api/user/123'), []);
That's three identical network requests hitting your server at nearly the same time—wasteful and potentially expensive.
How TanStack Query Deduplicates
When multiple components call useQuery with the same query key during the same render cycle, TanStack Query recognizes this and makes only one network request. All components share the result:
// Component A
useQuery({ queryKey: ['user', 123], queryFn: fetchUser });
// Component B
useQuery({ queryKey: ['user', 123], queryFn: fetchUser });
// Component C
useQuery({ queryKey: ['user', 123], queryFn: fetchUser });
Behind the scenes, TanStack Query sees ['user', 123] three times, initiates only one fetchUser call, and updates all three components when the data arrives.
The Analogy
Think of it like ordering pizza with roommates. Instead of each person calling the restaurant separately for the same order, one person makes the call and everyone gets their slice from the same delivery.
When Deduplication Happens
Deduplication works automatically when:
- Multiple components mount at roughly the same time
- They use identical query keys
- The data isn't already cached (or is stale and refetching)
This happens without any configuration—it's built into TanStack Query's core behavior.
Key Takeaway: TanStack Query automatically deduplicates requests with the same query key, making only one network call even when multiple components need the same data simultaneously—saving bandwidth and reducing server load.