Sub topic 10
Where effect-based fetching falls short
This chapter assumes you can write a component that fetches in useEffect with loading and error states, abort on cleanup, and extract that logic into a useFetch hook. Here you will see what that approach still does not give you.
What the hook already solves
The useFetch hook handles request state, response.ok checks, and stale responses through AbortController. For one component that loads one resource, that is enough. The gaps appear when several components share data, or when users move around the app.
No shared cache
Each call to useFetch owns its own state. Two components that call useFetch('/api/users/1') send two requests and hold two copies of the response.
function Header({ userId }) {
const user = useFetch(`/api/users/${userId}`);
return <p>{user.data?.name}</p>;
}
function Sidebar({ userId }) {
const user = useFetch(`/api/users/${userId}`);
return <p>{user.data?.email}</p>;
}Both components mount together, so the browser sends the same request twice. If the user later navigates away and back, the component remounts, state resets to loading, and the request runs again. Data you already had is thrown away and the user sees a spinner.
No deduplication
Nothing coordinates requests across components. A hook has no way to learn that another instance is already fetching the same URL. Wrapping each component in a shared parent to pass data down fixes the duplicate, but it couples the layout to the data flow.
No refetching or staleness rules
The effect runs only when url changes. It does not refetch when:
- The browser tab regains focus after sitting idle.
- The network reconnects.
- Another component changes the data on the server.
Data on screen can stay out of date indefinitely. Adding these behaviors by hand means more effects, event listeners, and timers inside the hook.
Other gaps
- Retries. A failed request stays failed until the dependencies change. You must write retry and backoff logic yourself.
- Updates after writes. After a
PATCH, nothing tells other components to reload. You need to refetch manually or lift state up. - Waterfalls. Effects run after render. A child that fetches starts only after its parent has rendered and finished its own request, so nested fetches run in sequence.
- Loading on the client only. The request starts after JavaScript loads and the component mounts, which delays the first useful content.
What a fix needs to provide
Solving these problems requires state that lives outside any single component. That state needs a key to identify each piece of data, a record of when it was fetched, and rules for when to refetch it. Server data is a different kind of state from UI state such as an open menu. It is owned by the server, shared by many components, and can become out of date without the client knowing.
You could build this yourself with a context, a map of requests, and timers. Most teams use a library, which the next chapter covers.
Exercise
Two components on one page both call useFetch('/api/users/1'). Name two problems with this and say why a hook that only uses useState and useEffect cannot fix them.
Show answer
The page sends duplicate requests, and the data is not cached, so remounting the component refetches it and shows a loading state again. Each hook call keeps its own state, so instances cannot share responses or see each other's in-flight requests. A fix needs shared state outside the components, keyed by request.