Description
Create a small React app where users can toggle between "Light" and "Dark" themes. Use the Context API to manage the theme state across components.
// ThemeContext.js
import React, { createContext, useState } from 'react';
// 1. Create the Context
// 2. Create a Provider Component
export const ThemeProvider = ({ children }) => {
};
// 3. Consume context in header and impolement switch
const Header = () => {
return (
<header>
<h1>Theme Switcher</h1>
</header>
);
};
// 4. In the content change background of the section based on theme
const Content = () => {
return <div>My Content</div>
}
const App = () => {
return (
<ThemeProvider>
<div>
<Header />
<Content />
</div>
</ThemeProvider>
);
};
ThemeContext
in both Header
and Content
components.Header
component.Content
component to toggle the theme.