Introduction
In this project, you will learn how to create a React application that allows users to switch between light and dark mode. The project will demonstrate the usage of the React Context API and the useContext hook to manage the global theme state.
👀 Preview

🎯 Tasks
In this project, you will learn:
- How to create a
ThemeContextusing React's Context API - How to use the
ThemeContextin theAppcomponent to change the overall app style based on the theme - How to use the
ThemeContextin theCardcomponent to change the card styles based on the theme - How to wrap the
Appcomponent with theThemeProviderto make the theme context available throughout the application
🏆 Achievements
After completing this project, you will be able to:
- Understand how to use the React Context API to manage global state
- Apply the
useContexthook to access the context values - Implement different styles based on the current theme
- Toggle the theme and update the global state
Set Up the Project
In this step, you will learn how to set up the project and install the necessary dependencies.
Open your code editor and navigate to the project directory.
├── components
│ └── Card.js
├── contexts
│ └── ThemeContext.js
├── App.css
├── App.js
├── index.css
├── index.js
├── reportWebVitals.js
└── setupTests.js
File descriptions:
- The
componentsdirectory contains all component files. - The
contextsdirectory contains theThemeContext.jsfile that needs to be completed.
Since this is a React project, we need to install the dependencies by running the following command:
npm install
After installing the dependencies, follow these steps to access the project:
- Start a local server by running the following command in the terminal:
npm start
- Open the web server on the right side of the coding environment. You will see the static effect as shown below. Currently, clicking the "Dark Mode" button does not cause any changes.

Create the ThemeContext
In this step, you will learn how to create the ThemeContext using React's Context API. Follow the steps below to complete this step:
Open the
ThemeContext.jsfile located in thesrc/contextsdirectory.Create the
ThemeContextusingReact.createContext():export const ThemeContext = React.createContext();Create the
ThemeProvidercomponent that will wrap theAppcomponent and provide the theme context:export const ThemeProvider = (props) => { const [theme, setTheme] = React.useState("light"); return ( <ThemeContext.Provider value={{ theme, setTheme }}> {props.children} </ThemeContext.Provider> ); };The
ThemeProvidercomponent initializes thethemestate to "light" and provides thethemeandsetThemevalues to the context.
Use the ThemeContext in the App component
In this step, you will learn how to use the ThemeContext in the App component to change the overall app style based on the theme.
Open the
src/App.jsfile.Use the
useContexthook to access thethemevalue from theThemeContext:const { theme } = useContext(ThemeContext);Update the
Appcomponent's JSX to apply different styles based on thethemevalue:return ( <div className={theme === "light" ? "app app-light" : "app app-dark"}> <Card></Card> </div> );The
app-lightandapp-darkclasses will be applied to the maindivelement based on thethemevalue.
Use the ThemeContext in the Card component
In this step, you will learn how to use the ThemeContext in the Card component to change the card styles based on the theme.
Open the
src/components/Card.jsfile.Use the
useContexthook to access thethemeandsetThemevalues from theThemeContext:export const Card = () => { const { theme, setTheme } = useContext(ThemeContext); // ... };Implement the
handleThemeTogglefunction to toggle the theme:const handleThemeToggle = (e) => { setTheme(theme === "light" ? "dark" : "light"); };This function will update the
themevalue in theThemeContextwhen the button is clicked.
Wrap the App component with the ThemeProvider
In this final step, you will learn how to wrap the App component with the ThemeProvider to make the theme context available throughout the application.
Open the
src/index.jsfile.Import the
ThemeProviderfrom theThemeContext.jsfile:import { ThemeProvider } from "./contexts/ThemeContext";Wrap the
Appcomponent with theThemeProviderin theReactDOM.renderfunction:root.render( <React.StrictMode> <ThemeProvider> <App /> </ThemeProvider> </React.StrictMode> );
Now, the App component and its child components can access the theme and setTheme values from the ThemeContext using the useContext hook.
After completing the challenge, the effect should be as follows:

Congratulations! You have completed the project. You should now be able to switch between light and dark mode by clicking the button in the card component.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



