Consider the following React components using Context to avoid prop drilling. What will be rendered inside the <DisplayUser> component?
import React, { createContext, useContext } from 'react'; const UserContext = createContext(null); function DisplayUser() { const user = useContext(UserContext); return <p>{`User: ${user.name}`}</p>; } function Wrapper() { const user = { name: 'Alice' }; return ( <UserContext.Provider value={user}> <DisplayUser /> </UserContext.Provider> ); } export default Wrapper;
Look at how useContext gets the value from UserContext.Provider.
The UserContext.Provider passes the user object down. useContext(UserContext) inside DisplayUser accesses this object, so user.name is 'Alice'.
count after clicking the button twice in this component using Context?Given this React component using Context to share state, what will be the value of count after clicking the button two times?
import React, { createContext, useContext, useState } from 'react'; const CountContext = createContext(null); function IncrementButton() { const { count, setCount } = useContext(CountContext); return <button onClick={() => setCount(count + 1)}>Increment</button>; } function DisplayCount() { const { count } = useContext(CountContext); return <p>Count: {count}</p>; } function Counter() { const [count, setCount] = useState(0); return ( <CountContext.Provider value={{ count, setCount }}> <DisplayCount /> <IncrementButton /> </CountContext.Provider> ); } export default Counter;
Each button click increases count by 1 using setCount.
The initial count is 0. Clicking the button twice calls setCount(count + 1) twice, so count becomes 2.
Look at this React code using Context. The DisplayTheme component does not update when the theme changes. What is the cause?
import React, { createContext, useContext, useState, useCallback } from 'react'; const ThemeContext = createContext('light'); function DisplayTheme() { const theme = useContext(ThemeContext); return <p>Theme: {theme}</p>; } function ThemeSwitcher() { const [theme, setTheme] = useState('light'); const toggleTheme = useCallback(() => { setTheme(theme === 'light' ? 'dark' : 'light'); }, []); return ( <ThemeContext.Provider value={theme}> <DisplayTheme /> <button onClick={toggleTheme}>Toggle Theme</button> </ThemeContext.Provider> ); } export default ThemeSwitcher;
Think about how toggleTheme accesses theme inside the function.
The toggleTheme function closes over the initial theme value. It does not get the updated theme on subsequent renders, so toggling does not work.
Prop drilling means passing props through many layers of components. How does React Context help avoid this?
Think about how data flows with and without Context.
Context provides a way to pass data through the component tree without manually passing props at every level, avoiding prop drilling.
Choose the code snippet that correctly creates a React Context with default value 'blue' and uses it inside a component to display the color.
Remember how to create and consume context with createContext and useContext.
Option A correctly creates a context with default 'blue' and uses useContext(ColorContext) to consume it.
Option A incorrectly passes a second argument to useContext which is invalid.
Option A wrongly calls useContext as a method on the context object.
Option A calls useContext without arguments, causing an error.