Introduction

Dark mode has become a standard feature in modern web applications. Users prefer it for better readability, reduced eye strain, and improved battery performance on devices.

If you are building a modern UI, implementing a dark mode toggle in React is almost expected. The good news is that it is not complex if approached step by step with a clean structure.

In this guide, we will learn how to implement a dark mode toggle in React using state management, CSS variables, and persistent storage. This approach is widely used in React applications, UI/UX design systems, and modern frontend development.

What is Dark Mode in React?

Dark mode is a UI feature that switches the application theme from light colors to darker colors.

Instead of redesigning everything, we dynamically change styles based on a theme state.

Why Dark Mode is Important

How Dark Mode Works Internally

At a high level, dark mode works by:

In React, this is typically handled using state and CSS.

Step 1: Create a React Project

npx create-react-app dark-mode-app
cd dark-mode-app
npm start

Explanation

Step 2: Create Theme State

Inside your main component (App.js):

import { useState } from "react";

function App() {
  const [theme, setTheme] = useState("light");

  return (
    <div>
      <h1>Dark Mode Example</h1>
    </div>
  );
}

export default App;

Explanation

Step 3: Create Toggle Function

const toggleTheme = () => {
  setTheme(theme === "light" ? "dark" : "light");
};

Explanation

Step 4: Apply Theme Class to Root

return (
  <div className={theme}>
    <button onClick={toggleTheme}>Toggle Theme</button>
    <h1>Dark Mode Example</h1>
  </div>
);

Explanation

Step 5: Define CSS for Themes

Create styles in App.css:

.light {
  background-color: white;
  color: black;
}

.dark {
  background-color: #121212;
  color: white;
}

Explanation

Step 6: Use CSS Variables (Better Approach)

:root {
  --bg-color: white;
  --text-color: black;
}

.dark {
  --bg-color: #121212;
  --text-color: white;
}

body {
  background-color: var(--bg-color);
  color: var(--text-color);
}

Explanation

Step 7: Persist Theme Using Local Storage

import { useEffect } from "react";

useEffect(() => {
  const savedTheme = localStorage.getItem("theme");
  if (savedTheme) {
    setTheme(savedTheme);
  }
}, []);

useEffect(() => {
  localStorage.setItem("theme", theme);
}, [theme]);

Explanation

Step 8: Improve UX with Default System Preference

useEffect(() => {
  const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
  setTheme(prefersDark ? "dark" : "light");
}, []);

Explanation

Complete Example

import { useState, useEffect } from "react";
import "./App.css";

function App() {
  const [theme, setTheme] = useState("light");

  useEffect(() => {
    const savedTheme = localStorage.getItem("theme");
    if (savedTheme) {
      setTheme(savedTheme);
    }
  }, []);

  useEffect(() => {
    localStorage.setItem("theme", theme);
  }, [theme]);

  const toggleTheme = () => {
    setTheme(theme === "light" ? "dark" : "light");
  };

  return (
    <div className={theme}>
      <button onClick={toggleTheme}>Toggle Theme</button>
      <h1>Dark Mode in React</h1>
    </div>
  );
}

export default App;

Real-World Use Case

Many modern applications like dashboards, SaaS platforms, and developer tools implement dark mode to enhance usability.

Example scenarios:

Best Practices for Dark Mode in React

Use CSS Variables

Helps manage themes easily across components.

Avoid Hardcoding Colors

Always use variables for flexibility.

Maintain Accessibility

Ensure good contrast between text and background.

Test Across Devices

Check behavior on mobile and desktop.

Common Mistakes

Not Saving Theme

Leads to poor user experience.

Overusing Inline Styles

Makes code hard to maintain.

Ignoring System Preferences

Misses better UX opportunities.

Advantages of Dark Mode

Limitations

Summary

Implementing a dark mode toggle in React is a practical and essential feature for modern web applications. By using React state, CSS variables, and local storage, developers can create a seamless and user-friendly theme-switching experience. With proper implementation, dark mode enhances usability, improves accessibility, and aligns with modern UI/UX trends, making it a valuable addition to any React application.