Skip to content
Rung

08/12

No. 08Medium20 min

Context API: Theme Switcher

Mission

Solve prop drilling with the Context API so global data, like a visual theme, is available anywhere without passing props by hand.

Tasks

  1. 1

    Create and export a ThemeContext with createContext().

  2. 2

    Implement ThemeProvider so it owns the theme state ('light' or 'dark') and a toggleTheme function.

  3. 3

    Pass both the theme and the toggle through the Provider's value prop.

  4. 4

    Wrap Navbar and Content in App.js with your ThemeProvider.

  5. 5

    Consume the context in Navbar.js and Content.js with useContext.