Discover how to share data effortlessly across your React app without endless prop passing!
Creating context in React - Why You Should Know This
Imagine you have a React app with many components, and you want to share user settings like theme or language across them all.
You try passing these settings down through every component as props.
Passing props manually through many layers is tiring and error-prone.
If you add or remove components, you must update all those props everywhere.
This makes your code messy and hard to maintain.
Creating context lets you share data globally in your React app without passing props manually.
Any component can access the shared data directly, making your code cleaner and easier to update.
function App() {
return <Parent user={user} />;
}
function Parent({ user }) {
return <Child user={user} />;
}
function Child({ user }) {
return <Display user={user} />;
}const UserContext = React.createContext();
function App() {
return <UserContext.Provider value={user}>
<Child />
</UserContext.Provider>;
}
function Child() {
const user = React.useContext(UserContext);
return <Display user={user} />;
}It enables easy sharing of data across many components without messy prop passing.
Think of a website where the user chooses a dark or light theme once, and every part of the site updates automatically without extra code.
Passing props through many components is hard and messy.
Creating context shares data globally in React apps.
It makes your code cleaner and easier to maintain.
Practice
createContext() function?Solution
Step 1: Understand what
This function creates a context object that can hold data to be shared across components without passing props manually.createContext()doesStep 2: Compare with other options
Creating components, fetching data, or managing local state are unrelated tocreateContext().Final Answer:
To create a context object that allows sharing data without passing props manually -> Option DQuick Check:
Purpose of createContext() = share data without props [OK]
- Confusing context creation with component creation
- Thinking it fetches data from APIs
- Mixing it up with useState for local state
Solution
Step 1: Identify correct syntax for creating context
The correct way is to callcreateContext()from the React object:React.createContext().Step 2: Check other options
const MyContext = createContext(); misses the React prefix, which causes an error unless imported separately. Options A and D useuseContext(), which is for consuming context, not creating it.Final Answer:
const MyContext = React.createContext(); -> Option BQuick Check:
Creating context = React.createContext() [OK]
- Using useContext() instead of createContext()
- Omitting React prefix without import
- Confusing creation and consumption functions
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>
);
}Solution
Step 1: Understand context default and provider value
The context default is "default", but theDisplaycomponent is inside a provider with value "hello".Step 2: Determine value consumed by useContext
SinceDisplayis inside the provider,useContext(MyContext)returns "hello".Final Answer:
"hello" -> Option AQuick Check:
Provider value overrides default = "hello" [OK]
- Assuming default value is always used
- Thinking useContext errors without provider
- Confusing string quotes in output
const ThemeContext = React.createContext('light');
function Button() {
const theme = React.useContext();
return <button>Theme is {theme}</button>;
}Solution
Step 1: Check useContext usage
TheuseContexthook requires the context object as an argument. Here, it is called without any argument.Step 2: Verify other options
Context is correctly created withcreateContext. Provider is optional but not an error here. Functional components can use hooks, so no need for class-based.Final Answer:
Missing argument in useContext call -> Option CQuick Check:
useContext requires context argument [OK]
- Calling useContext() without argument
- Confusing createContext and useContext
- Thinking Provider is mandatory to avoid errors
Solution
Step 1: Check context creation and default value
const ThemeContext = React.createContext('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>; } correctly creates context with default "light" usingcreateContext('light').Step 2: Verify provider and consumer usage
The provider passes "dark" as value, and consumer usesuseContext(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 theReact.prefix onuseState, 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 callsReact.useContext('light')instead ofcreateContext(). 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>; } callsuseContext()without argument, causing error.Final Answer:
Correctly creates and uses context with provider and consumer -> Option AQuick Check:
Correct createContext and useContext with provider = const ThemeContext = React.createContext('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>; } [OK]
- Using useContext instead of createContext to create context
- Calling useContext without argument
- Not passing provider value or missing provider
Ready to prove it?
Pass the test and this pattern moves unknown
