Introduction
Context lets you share data easily across many parts of your app without passing props step-by-step.
Context lets you share data easily across many parts of your app without passing props step-by-step.
import { createContext } from 'react'; const MyContext = createContext(defaultValue); export default MyContext;
createContext makes a new context object.
You can give a defaultValue used when no provider is found.
import { createContext } from 'react'; const ThemeContext = createContext('light');
null.import { createContext } from 'react'; const UserContext = createContext(null);
This example shows how to create a context for language settings, provide it to components, and update it from a child component.
import React, { createContext, useState, useContext } from 'react'; // 1. Create context with default value const LanguageContext = createContext('English'); // 2. Create a provider component function LanguageProvider({ children }) { const [language, setLanguage] = useState('English'); return ( <LanguageContext.Provider value={{ language, setLanguage }}> {children} </LanguageContext.Provider> ); } // 3. Create a component that uses the context function LanguageSwitcher() { const { language, setLanguage } = useContext(LanguageContext); return ( <div> <p>Current language: {language}</p> <button onClick={() => setLanguage('Spanish')}>Switch to Spanish</button> <button onClick={() => setLanguage('English')}>Switch to English</button> </div> ); } // 4. Use provider at top level export default function App() { return ( <LanguageProvider> <LanguageSwitcher /> </LanguageProvider> ); }
Always wrap components that use context inside the provider.
Context is great for global data but avoid overusing it for everything.
Context shares data without passing props manually.
Create context with createContext().
Use a provider to supply data and useContext() to consume it.
createContext() function?createContext() doescreateContext().createContext() from the React object: React.createContext().useContext(), which is for consuming context, not creating it.const MyContext = React.createContext('default');
function Display() {
const value = React.useContext(MyContext);
console.log(value);
return null;
}
function App() {
return (
<MyContext.Provider value="hello">
<Display />
</MyContext.Provider>
);
}Display component is inside a provider with value "hello".Display is inside the provider, useContext(MyContext) returns "hello".const ThemeContext = React.createContext('light');
function Button() {
const theme = React.useContext();
return <button>Theme is {theme}</button>;
}useContext hook requires the context object as an argument. Here, it is called without any argument.createContext. Provider is optional but not an error here. Functional components can use hooks, so no need for class-based.createContext('light').useContext(ThemeContext) correctly. const ThemeContext = React.createContext('light');
function ThemeProvider({ children }) {
const [theme, setTheme] = React.useState('dark');
return (
<ThemeContext.Provider value={theme}>
{children}
</ThemeContext.Provider>
);
}
function DisplayTheme() {
const theme = React.useContext(ThemeContext);
return <div>Current theme: {theme}</div>;
} attempts to use state but misses the React. prefix on useState, causing ReferenceError. const ThemeContext = React.useContext('light');
function ThemeProvider({ children }) {
const theme = 'dark';
return (
<ThemeContext.Provider value={theme}>
{children}
</ThemeContext.Provider>
);
}
function DisplayTheme() {
const theme = React.useContext(ThemeContext);
return <div>Current theme: {theme}</div>;
} incorrectly calls React.useContext('light') instead of createContext(). const ThemeContext = React.createContext();
function ThemeProvider({ children }) {
const theme = 'dark';
return (
<ThemeContext.Provider value={theme}>
{children}
</ThemeContext.Provider>
);
}
function DisplayTheme() {
const theme = React.useContext();
return <div>Current theme: {theme}</div>;
} calls useContext() without argument, causing error.Pass the test and this pattern moves unknown