What will be displayed if DisplayTheme is rendered without a ThemeContext.Provider?
medium
A. Current theme: undefined
B. Current theme: light
C. Current theme: null
D. Error: Context provider missing
Solution
Step 1: Check default context value
The context is created with default value 'light', so if no Provider wraps the component, this default is used.
Step 2: Understand useContext behavior without Provider
useContext returns the default value when no Provider is found above in the tree.
Final Answer:
Current theme: light -> Option B
Quick Check:
Default context value used without Provider [OK]
Hint: Default context value shows if no Provider wraps component [OK]
Common Mistakes:
Expecting undefined or null instead of default value
Thinking it throws an error without Provider
Confusing default value with null
4. Identify the error in this Next.js client component using React context:
import { createContext, useContext } from 'react';
const UserContext = createContext();
export default function UserName() {
const user = useContext(UserContext);
return <div>User: {user.name}</div>;
}
medium
A. UserContext is not wrapped with a Provider, so user is undefined causing error
B. useContext hook is used incorrectly; should pass a string
C. Missing import of React from 'react'
D. Cannot use context inside client components
Solution
Step 1: Check context default value and Provider usage
UserContext is created without a default value and no Provider is shown wrapping UserName, so useContext returns undefined.
Step 2: Understand error cause
Accessing user.name when user is undefined causes a runtime error.
Final Answer:
UserContext is not wrapped with a Provider, so user is undefined causing error -> Option A
Quick Check:
Undefined context value causes error accessing properties [OK]
Hint: Always wrap context consumers with Provider to avoid undefined errors [OK]
Common Mistakes:
Assuming useContext needs a string argument
Forgetting to wrap with Provider
Thinking React import is mandatory in Next.js 13+ client components
5. You want to create a theme toggle in a Next.js client component using React context. Which approach correctly updates the theme value across components?
hard
A. Pass theme as a prop to every component without using context
B. Create ThemeContext with a fixed string; update theme by changing the string directly in components
C. Use useState inside each component separately without context to manage theme
D. Create a ThemeContext with state and provide toggle function via Provider; use useContext to read and update theme
Solution
Step 1: Understand state sharing with context
To update theme globally, context must provide state and a function to toggle it, so all components share and update the same state.
Step 2: Evaluate other options
Fixed string cannot update dynamically; separate useState per component does not share state; passing props everywhere defeats context purpose.
Final Answer:
Create a ThemeContext with state and provide toggle function via Provider; use useContext to read and update theme -> Option D
Quick Check:
Context with state and updater shares dynamic data [OK]
Hint: Use context Provider with state and updater for shared dynamic data [OK]
Common Mistakes:
Trying to update fixed context value directly
Using separate state in each component without sharing
Passing props everywhere instead of using context
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.