Dark Mode
React.js
easy
You have to build a simple Dark Mode Toggle component in React. The goal is to allow users to switch between light mode and dark mode by toggling a checkbox. The theme should visually update the background color , text and text color .
Things To Do
-
State Management
- Add a
useState
hook to keep track of whether the app is in dark mode or light mode.
- Add a
-
Toggle Function
- Implement the
toggleTheme
function to update the state when the checkbox is toggled.
- Implement the
-
Dynamic Class Application
- Apply the class
dark-mode
orlight-mode
to the parent<div>
based on the value of state variable.
- Apply the class
-
Text Label
- Update the
<span className="mode-text">
to show"Dark Mode"
or"Light Mode"
depending on the current theme.
- Update the
Important Note
- Apply the class
dark-mode
to the parent<div>
when dark mode is active. - Apply the class
light-mode
when light mode is active.
Reference UI
Preview what you need to build
Companies:
meesho
Solve Similar questions 🔥