Scoped theme providers

Context & Patterns — Advanced React challenge.

Build a ThemeContext where ThemeProviders can be NESTED — a nested provider creates a genuinely independent scope, overriding the theme only for its own subtree, while everything outside it keeps reading the outer theme. Themed components must also apply real inline CSS matching the resolved theme.

Requirements

Signature

function ThemeProvider({ theme: "light" | "dark"; children: React.ReactNode }): JSX.Element
function useTheme(): { theme: "light" | "dark" }
function ThemedBox({ label: string }): JSX.Element

Example

<ThemeProvider theme="light">
  <ThemedBox label="stays light" />
  <ThemeProvider theme="dark">
    <ThemedBox label="scoped dark" />
  </ThemeProvider>
</ThemeProvider>
// first box: white background, dark text
// second box: dark background, white text — independent of the outer theme

Constraints

🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code
Loading workspace…