Ojasa Mirai

Ojasa Mirai

ReactJS

Loading...

Learning Level

🟒 BeginnerπŸ”΅ Advanced
🌍 Context API BasicsπŸ“¦ Creating ContextπŸ”Œ Context.ProviderπŸ” useContext Hook🎯 Passing Data with ContextπŸ”„ Context with useState⚑ Context PerformanceπŸ—οΈ Context Best Practices
Reactjs/Context/Context Provider

πŸ”Œ Context.Provider β€” Sharing Values with Components

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.


🎯 How Provider Works

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.

πŸ’‘ Provider Value Prop

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>

🎨 Real-World Example: Theme Provider

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.

πŸ“Š Provider Usage Patterns

PatternBest ForExample
Single valueSimple data`value="light"`
Object valueRelated data`value={{ user, updateUser }}`
At root levelApp-wide dataWrapping entire app
Nested ProvidersScoped dataMultiple regions with different values

πŸ”‘ Key Takeaways

  • βœ… Provider component supplies data to nested components
  • βœ… Use the `value` prop to pass data
  • βœ… Any component inside Provider can access the value
  • βœ… Put Providers high in your component tree for maximum reach
  • βœ… The value can be any JavaScript value
  • βœ… Multiple Providers can nest for different data regions
  • βœ… Value updates cause components using Context to re-render

Ready to practice? Challenges | Next: useContext Hook


Resources

Python Docs

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

Courses

PythonFastapiReactJSCloud

Β© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn