Since DisplayUser is inside the Provider, it receives the value "Alice" from context.
Final Answer:
"Alice" -> Option B
Quick Check:
Provider value overrides default context [OK]
Hint: Provider value overrides default context value [OK]
Common Mistakes:
Assuming default context value is always used
Thinking useContext returns undefined without explicit value
Confusing Provider and Consumer roles
4. What is the error in the following code that tries to consume a context?
const LanguageContext = React.createContext('English');
function Greeting() {
const language = useContext(LanguageContext);
return <h1>Hello in {language}</h1>;
}
function App() {
return <Greeting />;
}
medium
A. LanguageContext must be a function, not an object
B. Greeting component must be a class component
C. useContext is not imported or defined
D. Context value must be passed as a prop
Solution
Step 1: Check useContext usage
The code uses useContext but does not import or prefix it with React.
Step 2: Identify missing import or reference
Without importing useContext from React or using React.useContext, this causes a ReferenceError.
Final Answer:
useContext is not imported or defined -> Option C
Quick Check:
useContext must be imported or accessed via React [OK]
Hint: Always import or prefix useContext with React [OK]
Common Mistakes:
Forgetting to import useContext
Using useContext without React prefix or import
Assuming context value passed as prop automatically
5. You have a nested component structure where ThemeContext provides a value at the top level. You want to consume this context in a deeply nested component without passing props through intermediate components. Which approach correctly achieves this?
hard
A. Wrap the nested component tree with ThemeContext.Provider and use useContext(ThemeContext) inside the nested component
B. Pass the context value as props through every intermediate component until it reaches the nested component
C. Use useContext in the nested component without wrapping the tree in ThemeContext.Provider
D. Create a new context inside the nested component and use useContext on it
Solution
Step 1: Understand context provider role
The ThemeContext.Provider must wrap the component tree to supply the context value.
Step 2: Consume context in nested component
Inside the nested component, useContext(ThemeContext) reads the value without needing props passed down.
Final Answer:
Wrap the nested component tree with ThemeContext.Provider and use useContext(ThemeContext) inside the nested component -> Option A
Quick Check:
Provider wraps tree; useContext reads value [OK]
Hint: Wrap tree with Provider, consume with useContext inside [OK]
Common Mistakes:
Trying to use useContext without Provider
Passing props through all components instead of context
Creating new context inside nested component
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.