CodePark/ questions / Context
START PRACTICINGPRACTICE
Frontend question · React › Context

Button is rendered with no ThemeContext provider anywhere above it. What is theme?

const ThemeContext = createContext('light');

function Button() {
  const theme = useContext(ThemeContext);
  return <button className={theme}>OK</button>;
}
'light'

theme is 'light'. With no provider above, useContext returns the default value passed to createContext.

"It throws" is the tempting wrong answer. React itself does not throw. Some libraries throw, but they do it in their own custom hook. The default value never changes. It is a fallback, not a way to update values.