Context solves prop drilling, but it is a dependency-injection mechanism, not a state manager. The main pitfalls are: every consumer re-renders when the provider's value changes by reference (and you can't subscribe to just part of it), creating a new value object on every render, putting fast-changing or unrelated state into one big context, hidden coupling that makes components hard to reuse and test, and forgetting the provider (you silently get the default value).
function AuthProvider({ children }) {
const [user, setUser] = useState(null);
// A new object on each render, so every consumer re-renders whenever AuthProvider does
return (
<AuthContext.Provider value={{ user, setUser }}>
{children}
</AuthContext.Provider>
);
}
Fix: memoize the value, or let the React Compiler do it.
const value = useMemo(() => ({ user, setUser }), [user]);
(React 19 also lets you write <AuthContext value={…}> directly. .Provider still works.)
useContext re-renders the consumer whenever the value changes, even if the component reads a field that didn't change. A context holding { theme, user, cart } re-renders theme-only components on every cart update. React.memo on the consumer doesn't help, because context bypasses props.
Mitigations:
ThemeContext, UserContext, CartContext).dispatch (which is stable) in its own context so "write-only" components never re-render.useSyncExternalStore). Context can still pass the store instance, which never changes.Context makes data implicitly global within a subtree. Overuse leads to:
children or element props so intermediate layers don't need the data at all.const ThemeContext = createContext('light');
If a consumer renders outside the provider it silently gets 'light'. For required contexts, default to null and throw in a custom hook:
export function useCart() {
const ctx = useContext(CartContext);
if (!ctx) throw new Error('useCart must be used inside <CartProvider>');
return ctx;
}
children don't re-render unless they consume the context, which is a useful optimization.'use client' file and render them as close to the leaves as practical.use(Context) can be called conditionally, unlike useContext.value, splitting contexts, and state/dispatch separation.Spotted a mistake or a rights concern? Report an issue