Consider this React code using context:
import React, { createContext, useContext } from 'react';
const MyContext = createContext('default');
function Display() {
const value = useContext(MyContext);
return <div>Value: {value}</div>;
}
export default function App() {
return (
<MyContext.Provider value="hello">
<Display />
</MyContext.Provider>
);
}What will be rendered inside the <div>?
import React, { createContext, useContext } from 'react'; const MyContext = createContext('default'); function Display() { const value = useContext(MyContext); return <div>Value: {value}</div>; } export default function App() { return ( <MyContext.Provider value="hello"> <Display /> </MyContext.Provider> ); }
Check what value is passed to the Provider and what useContext reads.
The MyContext.Provider sets the context value to "hello". The useContext(MyContext) hook reads this value, so the output is "Value: hello".
Given this React component using context and state:
import React, { createContext, useContext, useState } from 'react';
const CountContext = createContext();
function Counter() {
const { count, setCount } = useContext(CountContext);
return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
export default function App() {
const [count, setCount] = useState(0);
return (
<CountContext.Provider value={{ count, setCount }}>
<Counter />
</CountContext.Provider>
);
}After clicking the button two times, what is the value of count shown?
import React, { createContext, useContext, useState } from 'react'; const CountContext = createContext(); function Counter() { const { count, setCount } = useContext(CountContext); return <button onClick={() => setCount(count + 1)}>Count: {count}</button>; } export default function App() { const [count, setCount] = useState(0); return ( <CountContext.Provider value={{ count, setCount }}> <Counter /> </CountContext.Provider> ); }
Each click increments count by 1 starting from 0.
The initial count is 0. Each button click calls setCount(count + 1), increasing count by 1. After two clicks, count is 2.
Choose the correct code to create a React context with default value "guest".
Look for the correct way to call createContext with a default value.
The createContext function takes the default value as an argument. Option B correctly calls createContext('guest').
Examine this code snippet:
import React, { createContext, useContext } from 'react';
const ThemeContext = createContext();
function DisplayTheme() {
const theme = useContext(ThemeContext);
return <div>Theme: {theme.color}</div>;
}
export default function App() {
return <DisplayTheme />;
}What error will occur when this runs?
import React, { createContext, useContext } from 'react'; const ThemeContext = createContext(); function DisplayTheme() { const theme = useContext(ThemeContext); return <div>Theme: {theme.color}</div>; } export default function App() { return <DisplayTheme />; }
Check what value useContext returns when no Provider is used.
Since ThemeContext has no default value and no Provider wraps DisplayTheme, useContext(ThemeContext) returns undefined. Accessing theme.color causes a TypeError.
In React, when a context Provider's value prop changes, what happens to components consuming that context?
Think about how React compares context values to decide re-rendering.
React re-renders consuming components only when the context value changes by reference (shallow comparison). If the value is the same object, no re-render occurs.