⚛️ Introduction
Dark mode has become a must-have feature in modern web apps 🌑.
It’s not just about style — it improves accessibility, reduces eye strain, and adds a sleek aesthetic to your app ✨.
In this article, we’ll build a Dark Mode Toggle feature in React using the Context API, so that theme changes are shared across your entire app — easily and efficiently! ⚡
🧠 Why Use Context API for Dark Mode?
Normally, you can manage theme toggles with useState, but if you have multiple components that need to know the theme (like Navbar, Footer, Cards, etc.), passing props everywhere becomes messy. 😩
That’s where React’s Context API comes in!
It lets you:
Share theme state across components 🌍
Avoid prop drilling 🔁
Keep your code clean and scalable 💡
🏗️ Step 1. Set up a React App
If you don’t already have a React app, create one:
npx create-react-app dark-mode-toggle
cd dark-mode-toggle
npm startNow, you’re ready to build the dark mode feature! 🚀
⚙️ Step 2. Create a Theme Context
Inside your src folder, create a new file:
📁 ThemeContext.js
import { createContext, useState, useContext } from "react";
// Step 1: Create the context
const ThemeContext = createContext();
// Step 2: Create a provider component
export function ThemeProvider({ children }) {
const [theme, setTheme] = useState("light");
const toggleTheme = () => {
setTheme((prevTheme) => (prevTheme === "light" ? "dark" : "light"));
};
return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
{children}
</ThemeContext.Provider>
);
}
// Step 3: Custom hook to use the Theme Context easily
export function useTheme() {
return useContext(ThemeContext);
}
✅ Explanation
ThemeContext holds our theme state (light or dark)
ThemeProvider wraps our entire app and provides theme info
toggleTheme switches between light and dark modes
useTheme() is a helper hook for easy access
🌈 Step 3. Wrap Your App with ThemeProvider
Now open index.js (or main.jsx if using Vite) and wrap your app inside the ThemeProvider.
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { ThemeProvider } from "./ThemeContext";
import "./index.css";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<ThemeProvider>
<App />
</ThemeProvider>
);
🎯 This ensures that every component in your app can access the theme context.
💡 Step 4. Create a Toggle Button Component
Now let’s create a button to switch between dark and light themes.
📁 ThemeToggle.js
import React from "react";
import { useTheme } from "./ThemeContext";
function ThemeToggle() {
const { theme, toggleTheme } = useTheme();
return (
<button
onClick={toggleTheme}
style={{
padding: "10px 20px",
borderRadius: "10px",
border: "none",
cursor: "pointer",
backgroundColor: theme === "light" ? "#333" : "#fff",
color: theme === "light" ? "#fff" : "#333",
transition: "all 0.3s ease",
}}
>
{theme === "light" ? "🌙 Dark Mode" : "☀️ Light Mode"}
</button>
);
}
export default ThemeToggle;

Join the conversation! Your thoughts help the community grow.