Context helps share data easily between parts of a React app without passing props step-by-step.
You want to share user login info across many components.
You need a theme (like dark or light mode) available everywhere.
You want to avoid passing the same data through many layers of components.
You have settings or language preferences used in many places.
You want to keep your code clean and simple by reducing prop drilling.
Syntax
React
const MyContext = React.createContext(defaultValue);
function MyProvider({ children }) {
const value = ...; // some shared datareturn <MyContext.Provider value={value}>{children}</MyContext.Provider>;
}
function MyComponent() {
const contextValue = React.useContext(MyContext);
return <div>{contextValue}</div>;
}
React.createContext creates a context object.
Provider wraps components to share data.
Examples
This example shares a theme value "dark" to a button deep inside components without passing props.
React
const ThemeContext = React.createContext('light');
function App() {
return (
<ThemeContext.Provider value="dark">
<Toolbar />
</ThemeContext.Provider>
);
}
function Toolbar() {
return <ThemeButton />;
}
function ThemeButton() {
const theme = React.useContext(ThemeContext);
return <button>{`Theme is ${theme}`}</button>;
}
This example shares user info so any component can greet the user without prop drilling.
React
const UserContext = React.createContext(null);
function UserProvider({ children }) {
const user = { name: 'Anna' };
return <UserContext.Provider value={user}>{children}</UserContext.Provider>;
}
function Greeting() {
const user = React.useContext(UserContext);
return <h1>{`Hello, ${user.name}!`}</h1>;
}
Sample Program
This React app uses context to share the current language and a way to change it. The LanguageSwitcher component can read and update the language without props.
useContext requires the context object as an argument, e.g., useContext(ThemeContext).
Step 2: Verify other statements
Context creation outside component is correct. Default value 'light' is valid. Functional components can use hooks.
Final Answer:
useContext is called without context argument -> Option A
Quick Check:
useContext needs context argument = B [OK]
Hint: Always pass context object to useContext() [OK]
Common Mistakes:
Calling useContext() with no arguments
Creating context inside component repeatedly
Thinking default value must be an object
Believing hooks require class components
5. You want to share a user's login status and theme preference across many nested components without passing props at every level. Which approach best solves this problem?
hard
A. Use React Context to provide user and theme data globally
B. Pass user and theme as props through every component manually
C. Store user and theme in local component state only
D. Use CSS variables to store user and theme data
Solution
Step 1: Identify the problem with prop drilling
Passing props manually through many nested components is tedious and error-prone.
Step 2: Use Context for global data sharing
React Context allows sharing data like user login and theme globally without prop drilling.
Step 3: Evaluate other options
Local state is limited to one component. CSS variables cannot store user login info. Passing props manually is inefficient.
Final Answer:
Use React Context to provide user and theme data globally -> Option A
Quick Check:
Context shares global data easily = D [OK]
Hint: Use Context for global shared data like user and theme [OK]
Common Mistakes:
Passing props through every component manually
Trying to store user info in CSS
Using local state for global data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.