B. The displayed number increases by 1 each button press
C. The app crashes due to missing context
D. The button does not respond to presses
Solution
Step 1: Understand context and state connection
The count state is provided via CountContext.Provider and updated on button press.
Step 2: Trace button press effect
Pressing the button calls setCount(count + 1), updating count and re-rendering Display with new value.
Final Answer:
The displayed number increases by 1 each button press -> Option B
Quick Check:
Context updates with state changes = B [OK]
Hint: State update triggers context value change and re-render [OK]
Common Mistakes:
Assuming context value never changes
Thinking button does not update state
Believing app crashes without provider
4. Identify the bug in this React Native code that tries to use global state but still requires prop drilling:
const UserContext = React.createContext();
function Profile({ user }) {
return {user.name};
}
function App() {
const user = { name: 'Anna' };
return (
);
}
medium
A. Profile component should use useContext(UserContext) instead of props
B. UserContext.Provider is missing a value prop
C. User object is not defined
D. Profile component should be a class component
Solution
Step 1: Check how Profile receives data
Profile expects a user prop but is rendered without props, causing undefined user.
Step 2: Use context to avoid prop drilling
Profile should use useContext(UserContext) to get user directly from global state.
Final Answer:
Profile component should use useContext(UserContext) instead of props -> Option A
Quick Check:
Use context hook to avoid prop drilling = A [OK]
Hint: Use useContext inside components to get global state [OK]
Common Mistakes:
Forgetting to use useContext in child components
Assuming Provider needs no value prop
Thinking class components are required
5. You have a React Native app with deeply nested components needing the same user data. Which approach best avoids prop drilling and keeps code clean?
hard
A. Use React Context to create a global user state and access it with useContext in any component
B. Pass user data as props through every nested component manually
C. Store user data in local component state of the top-level component only
D. Use multiple useState hooks in each nested component to hold user data
Solution
Step 1: Understand prop drilling problem
Passing props through many nested components is tedious and error-prone.
Step 2: Use global state with React Context
React Context allows sharing user data globally, so any component can access it directly without prop passing.
Final Answer:
Use React Context to create a global user state and access it with useContext in any component -> Option A
Quick Check:
Global state with context avoids prop drilling = A [OK]
Hint: React Context shares data globally, no prop passing needed [OK]
Common Mistakes:
Passing props through every nested component
Using local state only at top level
Duplicating state in nested components
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.