0
0
Reactframework~20 mins

Why context is needed in React - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Context Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use React Context instead of props?

Imagine you have a React app with many nested components. You want to share a theme color across all of them.

Why is React Context helpful here compared to passing props?

AContext lets you avoid passing props through every intermediate component, making code cleaner and easier to maintain.
BContext automatically updates the browser URL to reflect the theme color.
CContext forces all components to re-render even if they don't use the shared data.
DContext replaces the need for useState in components.
Attempts:
2 left
💡 Hint

Think about how props must be passed down through each level even if intermediate components don't need the data.

component_behavior
intermediate
2:00remaining
What happens when a Context value changes?

Given a React Context Provider wrapping components, what happens when the Provider's value changes?

React
const ThemeContext = React.createContext('light');

function App() {
  const [theme, setTheme] = React.useState('light');
  return (
    <ThemeContext.Provider value={theme}>
      <Toolbar />
      <button onClick={() => setTheme('dark')}>Dark Mode</button>
    </ThemeContext.Provider>
  );
}

function Toolbar() {
  return <ThemedButton />;
}

function ThemedButton() {
  const theme = React.useContext(ThemeContext);
  return <button>{theme}</button>;
}
AThe Context value changes but components must manually update to see it.
BOnly the Provider component re-renders, children stay the same.
CAll components using the Context value re-render with the new value.
DNo components re-render unless useState is called inside them.
Attempts:
2 left
💡 Hint

Think about how React updates components that consume context when the Provider's value changes.

📝 Syntax
advanced
2:00remaining
Identify the correct way to consume React Context

Which option correctly uses React Context inside a functional component?

React
const UserContext = React.createContext(null);

function Profile() {
  // Fill in the blank
}
Aconst user = React.useContext(UserContext); return <div>{user.name}</div>;
Bconst user = useContext(UserContext); return <div>{user.name}</div>;
Cconst user = React.Context(UserContext); return <div>{user.name}</div>;
Dconst user = React.getContext(UserContext); return <div>{user.name}</div>;
Attempts:
2 left
💡 Hint

Remember the exact React hook name and how to call it.

🔧 Debug
advanced
2:00remaining
Why does this Context consumer not update on Provider change?

Consider this code:

const CountContext = React.createContext(0);

const Counter = React.memo(function() {
  const count = React.useContext(CountContext);
  return <div>Count: {count}</div>;
});

function App() {
  const [count, setCount] = React.useState(0);
  return (
    <CountContext.Provider value={count}>
      <Counter />
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </CountContext.Provider>
  );
}

But when clicking the button, the Counter does not update. Why?

AThe initial context value is 0, so React ignores updates to the Provider.
BThe Provider's value is not changing because setCount is not called correctly.
CuseContext hook is used incorrectly inside Counter.
DThe Counter component is wrapped in React.memo without props, so it doesn't re-render on context change.
Attempts:
2 left
💡 Hint

Think about how React.memo affects re-rendering when context changes.

lifecycle
expert
3:00remaining
When does a React Context Provider update trigger re-render?

In React, under which condition does a Context Provider cause its consumers to re-render?

AOnly when the Provider component unmounts and remounts.
BWhen the Provider's <code>value</code> prop changes by reference equality (===) compared to previous render.
CWhen any state inside the Provider component changes, regardless of <code>value</code> prop.
DWhen the Provider's <code>value</code> prop changes in deep equality but not reference equality.
Attempts:
2 left
💡 Hint

React compares the value prop by reference to decide if consumers update.