
ReactJS
Every Context has a Provider component. The Provider wraps your component tree and supplies data to all descendants. Any component inside the Provider can access that data.
The Provider component accepts a `value` prop. Everything you put in `value` becomes available to all nested components:
import { createContext } from "react";
const ThemeContext = createContext("light");
function App() {
return (
<ThemeContext.Provider value="dark">
<Header />
<Content />
<Footer />
</ThemeContext.Provider>
);
}
// All components inside Provider can access "dark"
function Header() {
// Can access theme here
return <h1>Header</h1>;
}The Provider doesn't automatically pass anything downβit just makes the value available. Child components must explicitly request it using the `useContext()` hook.
The `value` prop can be any JavaScript value: a string, object, array, or function. Most often it's an object containing multiple related values:
import { createContext, useState } from "react";
const UserContext = createContext();
function App() {
const [user, setUser] = useState({ name: "Alice", id: "123" });
// Pass an object with multiple related values
const value = {
user,
updateUser: (newName) => setUser({ ...user, name: newName }),
};
return (
<UserContext.Provider value={value}>
<UserProfile />
</UserContext.Provider>
);
}<details>
<summary>π More Examples</summary>
// Example 1: Array value
const ListContext = createContext([]);
function App() {
const items = ["apple", "banana", "orange"];
return (
<ListContext.Provider value={items}>
<ShoppingList />
</ListContext.Provider>
);
}
// Example 2: Multiple values in object
const ConfigContext = createContext();
function App() {
return (
<ConfigContext.Provider
value={{
apiUrl: "https://api.example.com",
timeout: 5000,
retries: 3,
}}
>
<ApiClient />
</ConfigContext.Provider>
);
}</details>
A common pattern is wrapping your whole app with a Theme Provider at the top level:
import { createContext, useState } from "react";
const ThemeContext = createContext();
function ThemeProvider({ children }) {
const [isDark, setIsDark] = useState(false);
const toggleTheme = () => setIsDark(!isDark);
const value = {
isDark,
toggleTheme,
backgroundColor: isDark ? "#1a1a1a" : "#ffffff",
textColor: isDark ? "#ffffff" : "#000000",
};
return (
<ThemeContext.Provider value={value}>
{children}
</ThemeContext.Provider>
);
}
function App() {
return (
<ThemeProvider>
<Header />
<MainContent />
<Footer />
</ThemeProvider>
);
}This pattern makes `ThemeContext` available throughout your entire app without needing to pass props.
| Pattern | Best For | Example |
|---|---|---|
| Single value | Simple data | `value="light"` |
| Object value | Related data | `value={{ user, updateUser }}` |
| At root level | App-wide data | Wrapping entire app |
| Nested Providers | Scoped data | Multiple regions with different values |
Ready to practice? Challenges | Next: useContext Hook
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep β’ Build Real β’ Verify Skills β’ Launch Forward