React Context is used to share data across components without passing props manually at every level. It helps avoid 'prop drilling' by providing a way to pass data through the component tree.
Click to reveal answer
beginner
How do you create a new Context in React?
You create a new Context by calling React.createContext(). This returns an object with Provider and Consumer components.
Click to reveal answer
beginner
What is the role of the Context Provider?
The Provider component supplies the context value to its child components. Any component inside the Provider can access the shared data.
Click to reveal answer
beginner
How do components consume context values in React?
Components can consume context values using the useContext hook or the Context.Consumer component.
Click to reveal answer
intermediate
Why should you avoid updating context value too often?
Updating context value too often causes all consuming components to re-render, which can hurt performance. It's best to keep context values stable or memoized.
Click to reveal answer
Which React function creates a new context?
AReact.useContext()
BReact.createContext()
CReact.createProvider()
DReact.useProvider()
✗ Incorrect
React.createContext() creates a new context object with Provider and Consumer.
What component provides the context value to children?
AContext.Provider
BuseContext
CContext.Consumer
DReact.Fragment
✗ Incorrect
Context.Provider wraps children and supplies the context value.
How do you access context value inside a functional component?
AUsing useContext hook
BUsing Context.Provider
CUsing React.createContext()
DUsing setState
✗ Incorrect
useContext hook reads the current context value inside functional components.
What problem does React Context help solve?
AManaging component state locally
BHandling events
CStyling components
DAvoiding prop drilling
✗ Incorrect
Context helps avoid passing props through many layers (prop drilling).
What happens if the context value changes?
AOnly Provider re-renders
BReact throws an error
CAll consuming components re-render
DNothing changes
✗ Incorrect
All components using the context will re-render when the value changes.
Explain how to create and use React Context to share data between components.
Think about how to share data without passing props step-by-step.
You got /4 concepts.
Describe why and when you should use React Context instead of props.
Consider a family sharing a secret without telling everyone individually.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of React's createContext() function?
easy
A. To manage local component state
B. To create a new React component
C. To fetch data from an API
D. To create a context object that allows sharing data without passing props manually
Solution
Step 1: Understand what createContext() does
This function creates a context object that can hold data to be shared across components without passing props manually.
Step 2: Compare with other options
Creating components, fetching data, or managing local state are unrelated to createContext().
Final Answer:
To create a context object that allows sharing data without passing props manually -> Option D
Quick Check:
Purpose of createContext() = share data without props [OK]
Hint: Remember: createContext() sets up shared data space [OK]
Common Mistakes:
Confusing context creation with component creation
Thinking it fetches data from APIs
Mixing it up with useState for local state
2. Which of the following is the correct way to create a context in React?
easy
A. const MyContext = useContext();
B. const MyContext = React.createContext();
C. const MyContext = createContext();
D. const MyContext = React.useContext();
Solution
Step 1: Identify correct syntax for creating context
The correct way is to call createContext() 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 use useContext(), which is for consuming context, not creating it.
Final Answer:
const MyContext = React.createContext(); -> Option B
Quick Check:
Creating context = React.createContext() [OK]
Hint: Use React.createContext() to create context, not useContext() [OK]
Common Mistakes:
Using useContext() instead of createContext()
Omitting React prefix without import
Confusing creation and consumption functions
3. Given the code below, what will be logged to the console?
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>
);
}
medium
A. "hello"
B. "default"
C. undefined
D. Error: useContext called outside provider
Solution
Step 1: Understand context default and provider value
The context default is "default", but the Display component is inside a provider with value "hello".
Step 2: Determine value consumed by useContext
Since Display is inside the provider, useContext(MyContext) returns "hello".
Final Answer:
"hello" -> Option A
Quick Check:
Provider value overrides default = "hello" [OK]
Hint: Provider value overrides default context value [OK]
Common Mistakes:
Assuming default value is always used
Thinking useContext errors without provider
Confusing string quotes in output
4. Identify the error in the following React context usage:
const ThemeContext = React.createContext('light');
function Button() {
const theme = React.useContext();
return <button>Theme is {theme}</button>;
}
medium
A. Provider is missing around Button
B. Context should be created with useContext
C. Missing argument in useContext call
D. Button component must be class-based to use context
Solution
Step 1: Check useContext usage
The useContext hook requires the context object as an argument. Here, it is called without any argument.
Step 2: Verify other options
Context is correctly created with createContext. 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 C
Quick Check:
useContext requires context argument [OK]
Hint: Always pass context object to useContext() [OK]
Common Mistakes:
Calling useContext() without argument
Confusing createContext and useContext
Thinking Provider is mandatory to avoid errors
5. You want to create a theme context that provides either "light" or "dark" mode to your app. Which approach correctly creates and uses this context with a provider and consumer component?
hard
A. 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>;
}
B. 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>;
}
C. 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>;
}
D. 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>;
}
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" using createContext('light').
Step 2: Verify provider and consumer usage
The provider passes "dark" as value, and consumer uses 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.
Final Answer:
Correctly creates and uses context with provider and consumer -> Option A
Quick 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]
Hint: createContext with default, provider passes value, useContext consumes [OK]
Common Mistakes:
Using useContext instead of createContext to create context
Calling useContext without argument
Not passing provider value or missing provider
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.