Multiple Providers of the Same Context
You'll learn: How nesting multiple Providers for the same context creates isolated scopes with different values.
The Core Idea
Just like you can have nested <div> elements, you can nest multiple Providers for the same context at different levels of your component tree. Each Provider creates a new scope, and components inside that scope read from the closest Provider above them—not the outermost one.
This creates "layered" or "isolated" regions where the context has different values.
Why This Matters
Imagine a theme context where most of your app uses a light theme, but you have a special notification panel that should always be dark—even when the app is in light mode. You can wrap just that panel in a second Provider with a dark theme value.
The Pattern
const ThemeContext = createContext('light');
function App() {
return (
<ThemeContext.Provider value="light">
<Header /> {/* Reads "light" */}
<ThemeContext.Provider value="dark">
<NotificationPanel /> {/* Reads "dark" */}
</ThemeContext.Provider>
<MainContent /> {/* Reads "light" */}
</ThemeContext.Provider>
);
}
The NotificationPanel and its children see "dark" because the inner Provider is closer. Components outside that nested Provider still see "light".
The Lookup Rule
React walks up the tree from the component that's consuming the context. It stops at the first Provider it finds. If there's no Provider, it uses the default value you passed to createContext.
Key Takeaway: Multiple Providers of the same context create nested scopes—components always read from the closest Provider above them, enabling isolated regions with different values.