Skip to content

QuizNot attempted

Nested providers: innermost wins

What theme value does the button render?
const ThemeContext = createContext('light');
function Button() {
const theme = useContext(ThemeContext);
return <button>{theme}</button>;
}
function App() {
return (
<ThemeContext.Provider value="dark">
<ThemeContext.Provider value="blue">
<Button />
</ThemeContext.Provider>
</ThemeContext.Provider>
);
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • blue
  • useContext reads the value from the nearest Provider above the component, so the inner blue provider wins.
  • Not light: the default is used only with no provider at all.
  • Not dark: the outer provider is shadowed.
  • Not 'throws': nesting providers of the same context is normal (e.g. themed sections).