Multiple Contexts in One Component
What You'll Learn: How to consume multiple different contexts simultaneously in a single component using multiple useContext calls.
The Core Idea
Just like a component can call useState multiple times, it can also call useContext multiple times. Each call subscribes to a different context, allowing your component to access values from completely separate sources of shared state.
Think of it like tuning into multiple radio stations at once — each context is a different broadcast, and your component has multiple receivers listening to each one independently.
How It Works
You simply call useContext once per context you need:
const ThemeContext = createContext();
const UserContext = createContext();
function MyComponent() {
const theme = useContext(ThemeContext);
const user = useContext(UserContext);
return (
<div style={{ background: theme.backgroundColor }}>
Welcome, {user.name}!
</div>
);
}
Each useContext call is independent. If ThemeContext updates, the component re-renders. If UserContext updates, the component re-renders. They're completely separate subscriptions.
Common Use Cases
- Theme + User: Styling based on theme while displaying user information
- Auth + Settings: Checking authentication status while reading app preferences
- Localization + Feature Flags: Translating UI while conditionally showing features
Why This Matters
This pattern lets you compose your application's concerns cleanly. You don't need one massive "global state" context — you can split responsibilities into focused contexts (theme, auth, data, etc.) and consume only what each component needs.
Key Takeaway: Multiple useContext calls in one component let you subscribe to multiple independent contexts — each call returns its context's current value, and any update to any consumed context triggers a re-render.