Sub topic 13
When context is the wrong tool
This chapter assumes you know createContext, Providers, useContext, and how a context value change re-renders its consumers. Context solves prop drilling, but it is often used where something simpler fits better. Here is how to tell.
What context does not do
Context is a way to deliver a value to a subtree. It is not a state manager. It has no selectors, so a component cannot subscribe to only part of the value. If a component calls useContext(AppContext) and any field of the value changes, that component re-renders.
That leads to the most common problem. Teams put everything in one large context (user, cart, theme, notifications). Every consumer then re-renders on every change. Memoizing the value and splitting contexts, covered in earlier chapters, reduce the cost. They do not remove it.
Pass props when the tree is shallow
Passing props through two or three layers is normal React. It makes data flow visible: you can read a component and see what it depends on. Context hides that dependency. Reach for context when props pass through many layers that do not use them, not at the first extra layer.
Use composition before context
Often the drilling comes from structure, not from a missing feature. If a middle component only forwards a prop, pass it the finished element instead.
function Page({ user }) {
// Layout never needs to know about user.
return (
<Layout sidebar={<Avatar user={user} />}>
<Feed user={user} />
</Layout>
);
}
function Layout({ sidebar, children }) {
return (
<div className="layout">
<aside>{sidebar}</aside>
<main>{children}</main>
</div>
);
}
function Avatar({ user }) {
return <img alt={user.name} src={user.avatarUrl} />;
}
function Feed({ user }) {
return <p>Latest posts for {user.name}</p>;
}Layout receives sidebar and children and has no user prop. The data goes from Page straight to the components that use it. This removes the drilling with no Provider, no re-render surprises, and no hidden dependency.
Use a state library for frequent, fine-grained updates
Context fits values that change rarely and are read by many components, such as theme, locale, or the signed-in user. It fits poorly when state changes often (every keystroke, drag position, or live data tick) and many components read different slices of it. In that case each update re-renders every consumer.
Libraries such as Redux Toolkit, Zustand, and Jotai let a component subscribe to one slice of state, so it re-renders only when that slice changes. They also bring devtools and middleware. They add a dependency and concepts to learn, so adopt one when you measure a real problem, not by default.
Use a data-fetching library for server data
Data that comes from an API is server state. It needs caching, refetching, loading and error states, and deduplicated requests. Storing fetched results in a context with useState and useEffect means writing all of that yourself. Libraries such as TanStack Query or SWR handle it.
Decision guide
| Situation | Better choice |
|---|---|
| Data passes through one or two layers | Props |
| A middle component only forwards props | Composition (`children` or element props) |
| Rarely changing value read across the app | Context |
| Frequently changing state with many slice readers | External state library |
| Data fetched from a server | Data-fetching library |
A practical rule
Start with props. Restructure with composition if drilling hurts. Use context for values that are truly app-wide and change rarely. Move to an external library when profiling shows context causing re-renders you cannot fix by splitting.
Exercise
A component tree is App > Layout > Sidebar > UserMenu. Only UserMenu needs the user object, and Layout and Sidebar only pass it along. Name two ways to avoid prop drilling here and say which you would try first.
Show answer
You can create a UserContext and read it in UserMenu, or use composition: App builds <UserMenu user={user} /> and passes it to Layout as an element prop (or as children), which Layout hands to Sidebar to render. Try composition first. It needs no Provider and keeps the data flow visible. Use context if many components at different depths need the user.
Exercise
A form stores every field's text in one context. Typing in one field makes unrelated components re-render. Why does this happen, and what are two ways to fix it?
Show answer
Every component that calls useContext on that context re-renders whenever the value changes, and context offers no way to subscribe to one field. Fixes: keep each field's text in local state and lift only submitted values, or move the form state to a library with selectors such as Zustand. Splitting into several contexts also helps if the fields form natural groups.