{"id":7202,"date":"2025-06-24T01:32:26","date_gmt":"2025-06-24T01:32:26","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=7202"},"modified":"2025-06-24T01:32:26","modified_gmt":"2025-06-24T01:32:26","slug":"creating-a-theme-switcher-in-react-4","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/creating-a-theme-switcher-in-react-4\/","title":{"rendered":"Creating a Theme Switcher in React"},"content":{"rendered":"<h1>Creating a Theme Switcher in React<\/h1>\n<p>In modern web development, user experience is paramount. As a developer, one way to enhance the experience is by incorporating a theme switcher in your applications. This allows users to toggle between different themes\u2014typically a light and dark mode\u2014that suits their visual preferences. In this blog, we will walk through creating a simple yet effective theme switcher in a React application.<\/p>\n<h2>What is a Theme Switcher?<\/h2>\n<p>A theme switcher is a user interface component that enables users to change the appearance of the application based on their preference. It usually toggles between predefined styles, such as light and dark themes. This not only adds a personal touch but can also help reduce eye strain and improve accessibility.<\/p>\n<h2>Setting Up Your React Application<\/h2>\n<p>If you don\u2019t already have a React application set up, you can create one quickly using Create React App. Open your terminal and run:<\/p>\n<pre><code>npx create-react-app theme-switcher<\/code><\/pre>\n<p>Once your application is created, navigate to the project directory:<\/p>\n<pre><code>cd theme-switcher<\/code><\/pre>\n<p>Now, let&#8217;s install <strong>styled-components<\/strong> to facilitate styling in our React app:<\/p>\n<pre><code>npm install styled-components<\/code><\/pre>\n<h2>Creating the Theme Context<\/h2>\n<p>To manage our themes centrally, we will utilize React Context. This allows us to share theme data across our components without prop drilling.<\/p>\n<p>Create a new file named <strong>ThemeContext.js<\/strong> in the <strong>src<\/strong> folder:<\/p>\n<pre><code>touch src\/ThemeContext.js<\/code><\/pre>\n<p>In <strong>ThemeContext.js<\/strong>, set up a context to provide theme information:<\/p>\n<pre><code>import React, { createContext, useContext, useState } from 'react';<br><br>\nconst ThemeContext = createContext();<br><br>\nexport const useTheme = () =&gt; useContext(ThemeContext);<br><br>\nexport const ThemeProvider = ({ children }) =&gt; {<br>\n    const [theme, setTheme] = useState('light');<br><br>\n    const toggleTheme = () =&gt; {<br>\n        setTheme(prevTheme =&gt; (prevTheme === 'light' ? 'dark' : 'light'));<br>\n    };<br><br>\n    return (<br>\n        &lt;ThemeContext.Provider value={{ theme, toggleTheme }}&gt;<br>\n            {children}<br>\n        &lt;\/ThemeContext.Provider&gt;<br>\n    );<br>\n};<br><\/code><\/pre>\n<h2>Integrating the Theme Provider<\/h2>\n<p>Next, we will wrap our application in the <strong>ThemeProvider<\/strong> so that the theme context can be accessed throughout our component tree. Open the <strong>src\/index.js<\/strong> file and modify it as follows:<\/p>\n<pre><code>import React from 'react';<br>\nimport ReactDOM from 'react-dom';<br>\nimport '.\/index.css';<br>\nimport App from '.\/App';<br>\nimport { ThemeProvider } from '.\/ThemeContext';<br><br>\nReactDOM.render(<br>\n    &lt;ThemeProvider&gt;<br>\n        &lt;App \/&gt;<br>\n    &lt;\/ThemeProvider&gt;<br>\n, document.getElementById('root'));<br><\/code><\/pre>\n<h2>Creating the Theme Switcher Component<\/h2>\n<p>Now it&#8217;s time to create the component responsible for toggling the theme. Create a new file <strong>ThemeSwitcher.js<\/strong> in the <strong>src<\/strong> folder:<\/p>\n<pre><code>touch src\/ThemeSwitcher.js<\/code><\/pre>\n<p>Next, implement the theme switcher feature:<\/p>\n<pre><code>import React from 'react';<br>\nimport { useTheme } from '.\/ThemeContext';<br><br>\nconst ThemeSwitcher = () =&gt; {<br>\n    const { theme, toggleTheme } = useTheme();<br><br>\n    return (<br>\n        &lt;button onClick={toggleTheme}&gt;Switch to {theme === 'light' ? 'Dark' : 'Light'} Mode&lt;\/button&gt;<br>\n    );<br>\n};<br><br>\nexport default ThemeSwitcher;<br><\/code><\/pre>\n<h2>Implementing Styles for Themes<\/h2>\n<p>We will now use styled-components to create a simple theme management style. In your <strong>src<\/strong> directory, create a file called <strong>themes.js<\/strong>:<\/p>\n<pre><code>touch src\/themes.js<\/code><\/pre>\n<p>Populate <strong>themes.js<\/strong> with the theme definitions:<\/p>\n<pre><code>export const lightTheme = {<br>\n    background: '#ffffff',<br>\n    color: '#000000'<br>\n};<br><br>\nexport const darkTheme = {<br>\n    background: '#000000',<br>\n    color: '#ffffff'<br>\n};<br><\/code><\/pre>\n<h2>Applying Styles Using ThemeProvider<\/h2>\n<p>Now, we need to apply these themes using <strong>styled-components<\/strong>. In your <strong>App.js<\/strong> file, import the necessary modules.<\/p>\n<pre><code>import React from 'react';<br>\nimport { ThemeProvider as StyledThemeProvider } from 'styled-components';<br>\nimport { useTheme } from '.\/ThemeContext';<br>\nimport ThemeSwitcher from '.\/ThemeSwitcher';<br>\nimport { lightTheme, darkTheme } from '.\/themes';<br><br>\nconst App = () =&gt; {<br>\n    const { theme } = useTheme();<br>\n    return (<br>\n        &lt;StyledThemeProvider theme={theme === 'light' ? lightTheme : darkTheme}&gt;<br>\n            &lt;div style={{<br>\n                background: theme === 'light' ? lightTheme.background : darkTheme.background,<br>\n                color: theme === 'light' ? lightTheme.color : darkTheme.color,<br>\n                minHeight: '100vh',<br>\n                padding: '20px'<br>\n            }}&gt;<br>\n                &lt;h1&gt;Welcome to the Theme Switcher App&lt;\/h1&gt;<br>\n                &lt;ThemeSwitcher \/&gt;<br>\n            &lt;\/div&gt;<br>\n        &lt;\/StyledThemeProvider&gt;<br>\n    );<br>\n};<br><br>\nexport default App;<br><\/code><\/pre>\n<h2>Putting It All Together<\/h2>\n<p>Your directory structure should look like this:<\/p>\n<ul>\n<li>src\/<\/li>\n<li>ThemeContext.js<\/li>\n<li>ThemeSwitcher.js<\/li>\n<li>themes.js<\/li>\n<li>App.js<\/li>\n<\/ul>\n<p>Now, start your application with:<\/p>\n<pre><code>npm start<\/code><\/pre>\n<p>Your application should open in the default web browser, and you should see a button that allows switching between light and dark modes.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we have explored how to create a simple theme switcher in a React application using Context and styled-components. Not only does this feature add a layer of personalization for users, but it also contributes to a more accessible and flexible web experience. With just a bit of code, you can significantly enhance the user interface of your applications.<\/p>\n<p>Feel free to expand on this example by adding additional themes, animations, or even integrating local storage to remember user preferences across sessions. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a Theme Switcher in React In modern web development, user experience is paramount. As a developer, one way to enhance the experience is by incorporating a theme switcher in your applications. This allows users to toggle between different themes\u2014typically a light and dark mode\u2014that suits their visual preferences. In this blog, we will walk<\/p>\n","protected":false},"author":82,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[398],"tags":[224],"class_list":["post-7202","post","type-post","status-publish","format-standard","category-react","tag-react"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/82"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=7202"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7202\/revisions"}],"predecessor-version":[{"id":7203,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7202\/revisions\/7203"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=7202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=7202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=7202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}