Context helps share data easily across many parts of a React app without passing props everywhere.
Context best practices in React
import React, { createContext, useContext, useState } from 'react'; const MyContext = createContext(null); function MyProvider({ children }) { const [value, setValue] = useState(null); return ( <MyContext.Provider value={{ value, setValue }}> {children} </MyContext.Provider> ); } function MyComponent() { const context = useContext(MyContext); // use context.value or context.setValue }
Always create context outside components to avoid recreating it on every render.
Use a Provider to wrap components that need access to the context data.
const ThemeContext = createContext('light'); function App() { return ( <ThemeContext.Provider value="dark"> <Toolbar /> </ThemeContext.Provider> ); }
function Toolbar() { const theme = useContext(ThemeContext); return <div>The current theme is {theme}</div>; }
const UserContext = createContext(null); function UserProvider({ children }) { const [user, setUser] = useState(null); return <UserContext.Provider value={{ user, setUser }}>{children}</UserContext.Provider>; }
This example shows a language context that shares the current language and a way to change it. The LanguageSwitcher component reads and updates the language using context.
import React, { createContext, useContext, useState } from 'react'; const LanguageContext = createContext('English'); function LanguageProvider({ children }) { const [language, setLanguage] = useState('English'); return ( <LanguageContext.Provider value={{ language, setLanguage }}> {children} </LanguageContext.Provider> ); } 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> ); } export default function App() { return ( <LanguageProvider> <LanguageSwitcher /> </LanguageProvider> ); }
Keep context values simple to avoid unnecessary re-renders.
Do not put large objects or functions in context unless needed.
Use multiple contexts if you have different kinds of data to share.
Context helps share data without passing props through many layers.
Wrap components with a Provider to give them access to context data.
Use useContext hook inside components to read or update context values.
Practice
Solution
Step 1: Understand React Context role
React Context allows sharing data across many components without passing props at every level.Step 2: Compare with other options
Styling, local state, and routing are handled by other React features or libraries, not Context.Final Answer:
To share data between components without passing props manually -> Option DQuick Check:
Context shares data without prop drilling [OK]
- Confusing Context with styling or routing
- Thinking Context manages only local state
- Assuming Context replaces all state management
Solution
Step 1: Recall Context creation syntax
React Context is created using React.createContext() function.Step 2: Check other options
A uses invalid constructor, B misses React prefix, D calls a hook instead of creating context.Final Answer:
const MyContext = React.createContext(); -> Option CQuick Check:
Use React.createContext() to create context [OK]
- Omitting React prefix
- Using new keyword incorrectly
- Confusing useContext hook with createContext
const ThemeContext = React.createContext('light');
function DisplayTheme() {
const theme = React.useContext(ThemeContext);
console.log(theme);
return <div>Current theme: {theme}</div>;
}
function App() {
return (
<ThemeContext.Provider value="dark">
<DisplayTheme />
</ThemeContext.Provider>
);
}Solution
Step 1: Identify Provider value
The ThemeContext.Provider wraps DisplayTheme with value "dark".Step 2: Understand useContext behavior
useContext reads the nearest Provider value, so theme is "dark" inside DisplayTheme.Final Answer:
"dark" -> Option AQuick Check:
useContext reads Provider value, not default [OK]
- Assuming default value is used despite Provider
- Thinking useContext returns undefined without explicit value
- Confusing useContext with useState
const UserContext = React.createContext();
function Profile() {
const user = React.useContext(UserContext);
return <div>User: {user.name}</div>;
}
function App() {
return <Profile />;
}Solution
Step 1: Check for Provider wrapping
App renders Profile without wrapping it in UserContext.Provider, so context value is undefined.Step 2: Understand consequence in Profile
Accessing user.name when user is undefined causes runtime error.Final Answer:
UserContext.Provider is missing, so user is undefined causing error -> Option AQuick Check:
Missing Provider means context value is undefined [OK]
- Thinking createContext must have default value
- Believing useContext usage is incorrect here
- Assuming components can't access context directly
Solution
Step 1: Understand Context update pattern
To update shared data, pass a setter function (like from useState) in Context value.Step 2: Evaluate other options
Using local useState won't update shared theme; creating new Context breaks sharing; modifying Context object directly is invalid.Final Answer:
Pass a setter function in the Context value and call it inside the nested component -> Option BQuick Check:
Pass setter in Context to update shared state [OK]
- Trying to update Context value directly
- Using local state instead of shared state
- Creating multiple Contexts unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
