Lesson 2 of 30
A data-fetching library isn't syntax sugar over useEffect — it's solving a specific set of concurrency and caching problems that a hand-rolled fetch quietly gets wrong.
13 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
Two components on the same page both fetch `/api/users/42` independently via their own `useEffect` calls. What's the concrete downside compared to using a data-fetching library keyed on the user id?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
A React-style data-fetching library (the React Query/SWR family of tools — described here generically, not tied to one library's current API) exists to solve a specific, mechanical set of problems that a hand-rolled useEffect + fetch + useState pattern doesn't handle for free:
// Hand-rolled: two components, same endpoint, no coordination
function ProfileCard({ userId }: { userId: string }) {
const [user, setUser] = useState<User | null>(null);
useEffect(() => {
fetch(`/api/users/${userId}`).then((r) => r.json()).then(setUser);
}, [userId]);
// ...
}
function ProfileSidebar({ userId }: { userId: string }) {
const [user, setUser] = useState<User | null>(null);
useEffect(() => {
fetch(`/api/users/${userId}`).then((r) => r.json()).then(setUser);
}, [userId]);
// ...
}
If ProfileCard and ProfileSidebar both render for the same userId on the same page, this fires two independent network requests for identical data, and each keeps its own copy — if one refetches later, the other silently goes stale relative to it. A fetching library replaces this with a hook keyed on ["user", userId]: both components subscribing to that key share one in-flight request and one cached result, and a refetch from either place updates both.
"Why would you reach for a data-fetching library instead of useEffect and fetch?" is a real, common question, and naming deduplication, shared caching by key, and background refetching specifically — rather than a vague "it's easier" — is what shows the interviewer you understand the actual problem being solved, not just the tool's name.