How to create Switch in ReactJS? Last Updated : 25 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will learn how to create a switch in React that toggles between light and dark mode. We'll use Material-UI's Switch component to achieve this functionality. By the end of this guide, you'll be able to implement a theme switcher that allows users to seamlessly switch between light and dark themes in your application.Creating React Application And Installing Module:Step 1: Create a React application using the following command:npx create-react-app foldernameStep 2: After creating your project folder i.e. foldername, move to it using the following command:cd foldernameStep 3: After creating the ReactJS application, Install the material-ui modules using the following command:npm install @material-ui/coreProject Structure:Project StructureThe updated dependencies in package.json file will look like:"dependencies": { "@emotion/react": "^11.13.0", "@emotion/styled": "^11.13.0", "@mui/icons-material": "^5.16.4", "@mui/material": "^5.16.4", "react": "^18.2.0", "react-scripts": "5.0.1", },Example: Here is an example of how to create a light/dark mode switch using React and Material-UI JavaScript import React from 'react'; import { ThemeProvider, createTheme, CssBaseline } from '@mui/material'; import FormControlLabel from '@mui/material/FormControlLabel'; import Switch from '@mui/material/Switch'; const App = () => { // State to manage the theme mode const [darkMode, setDarkMode] = React.useState(false); // Function to handle theme toggle const handleThemeChange = (event) => { setDarkMode(event.target.checked); }; // Create a theme instance const theme = createTheme({ palette: { mode: darkMode ? 'dark' : 'light', }, }); return ( <ThemeProvider theme={theme}> <CssBaseline /> {/* This component normalizes the CSS across different browsers */} <div style={{ margin: 'auto', display: 'block', width: 'fit-content', padding: '20px' }}> <h3>How to use Switch Component for Light/Dark Mode in ReactJS?</h3> <FormControlLabel control={ <Switch checked={darkMode} onChange={handleThemeChange} color="primary" name="darkMode" /> } label="Switch Dark Mode" /> </div> </ThemeProvider> ); } export default App; Step to Run Application: Run the application using the following command from the root directory of the project:npm startOutput: Now open your browser and go to http://localhost:3000/, you will see the following output: Comment More infoAdvertise with us G gouravhammad Follow Improve Article Tags : ReactJS Similar Reads React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read 90+ React Projects with Source Code [2025] React, managed by Facebook and a vibrant community of developers and companies, is a JavaScript library designed to craft dynamic user interfaces. It empowers developers with concepts like React web apps, components, props, states, and component lifecycles. With a primary focus on building single-pa 12 min read Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie 15+ min read React Introduction ReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability.It is developed and maintained by Facebook.The latest version of React is React 19.Uses 8 min read React Hooks ReactJS Hooks are one of the most powerful features of React, introduced in version 16.8. They allow developers to use state and other React features without writing a class component. Hooks simplify the code, make it more readable, and offer a more functional approach to React development. With hoo 10 min read React JSX JSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi 6 min read React Router React Router is a library for handling routing and navigation in React JS Applications. It allows you to create dynamic routes, providing a seamless user experience by mapping various URLs to components. It enables navigation in a single-page application (SPA) without refreshing the entire page.This 6 min read ReactJS Virtual DOM ReactJS Virtual DOM is an in-memory representation of the actual DOM (Document Object Model). React uses this lightweight JavaScript object to track changes in the application state and efficiently update the actual DOM only where necessary.What is the Virtual DOM?The Virtual DOM (VDOM) is a lightwe 4 min read React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo 7 min read Like