How to Create a Toggle Switch in React as a Reusable Component ?
Last Updated :
12 Sep, 2024
Creating a reusable toggle switch in React enhances the UI. This component can customized as required with the props and can be reused. It can be done using CSS or CSS frameworks like MUI and tailwindCSS.
In this article, we’re going to create a Toggle Switch in React as a reusable component.
Prerequisites:
Approach
To Create a Toggle Switch in React as a Reusable Component
- First, Create a ToggleSwitch Component with a label prop.
- Define an input of type checkbox inside the component.
- Style the Switch using CSS :before, :after selectors.
- Add a label for the input and export the component to get the required output.
Steps to Create Reusable Toggle Switch
Step 1: Initialize React Project
Create a React application using the following command:
npx create-react-app toggle-switch
Step 2: Navigate to Project directory
After creating your project folder i.e. toggle-switch, move to it using the following command:
cd toggle-switch
Project Structure:

Step 3: Define Toogle Component
Create toggleSwitch file and define the toogle switch component, taking a chackbox input and add style to make it toggle switch.
const ToggleSwitch = ({ label }) => {
return (
<div className="container">
{label}{" "}
<div className="toggle-switch">
<input
type="checkbox"
className="checkbox"
name={label}
id={label}
/>
<label className="label" htmlFor={label}>
<span className="inner" />
<span className="switch" />
</label>
</div>
</div>
);
};
Step 4: Import and Use
Import the toggleswitch component in app.js file.
<ToggleSwitch label="ComponentName" />
Example: This example creates a ToggelSwitch component using a checkbox input and css classes for styling.
CSS
/* Filename: ./components/ToggleSwitch.css*/
.container {
text-align: center;
}
.toggle-switch {
position: relative;
width: 75px;
display: inline-block;
text-align: left;
top: 8px;
}
.checkbox {
display: none;
}
.label {
display: block;
overflow: hidden;
cursor: pointer;
border: 0 solid #bbb;
border-radius: 20px;
}
.inner {
display: block;
width: 200%;
margin-left: -100%;
transition: margin 0.3s ease-in 0s;
}
.inner:before,
.inner:after {
float: left;
width: 50%;
height: 36px;
padding: 0;
line-height: 36px;
color: #fff;
font-weight: bold;
box-sizing: border-box;
}
.inner:before {
content: "YES";
padding-left: 10px;
background-color: #060;
color: #fff;
}
.inner:after {
content: "NO";
padding-right: 10px;
background-color: #bbb;
color: #fff;
text-align: right;
}
.switch {
display: block;
width: 24px;
margin: 5px;
background: #fff;
position: absolute;
top: 0;
bottom: 0;
right: 40px;
border: 0 solid #bbb;
border-radius: 20px;
transition: all 0.3s ease-in 0s;
}
.checkbox:checked + .label .inner {
margin-left: 0;
}
.checkbox:checked + .label .switch {
right: 0px;
}
JavaScript
// Filename: App.js
import React, { Component } from "react";
import ToggleSwitch from "./components/ToggleSwitch";
class App extends Component {
render() {
return (
<React.Fragment>
<ToggleSwitch label="Notifications" />
<ToggleSwitch label="Subscribe" />
</React.Fragment>
);
}
}
export default App;
JavaScript
// Filename: ./components/ToggleSwitch.js
import React from "react";
import "./ToggleSwitch.css";
const ToggleSwitch = ({ label }) => {
return (
<div className="container">
{label}{" "}
<div className="toggle-switch">
<input type="checkbox" className="checkbox"
name={label} id={label} />
<label className="label" htmlFor={label}>
<span className="inner" />
<span className="switch" />
</label>
</div>
</div>
);
};
export default ToggleSwitch;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Conclusion
To create a toggle as reusable component define the component with state,checkbox input and custom switch styling. Add props to handle state and store it as a React Component. Import the component from defined path and use the ToggleSwitch in your applicaiton.
Similar Reads
How to create a rating component in ReactJS ?
Creating a rating component in React application allows user to register their review and rate the items using clickable stars. Using a custom rating component enhances the UI and experience of users.Prerequisite:Basic knowledge of npm or yarn.styled-components.Basic Knowledge of useState React hook
3 min read
How to use Switch Statement Inside a React Component?
When the switch statement is used within a React component it can greatly improve the readability and structure of the code. The logic of a switch statement can be clear and concise. This avoids complex and difficult nested if-else blocks. This leads to more maintainable and better-organized code. I
3 min read
How to create Class Component in React?
JavaScript syntax extension permits HTML-like code within JavaScript files, commonly used in React for defining component structure. It simplifies DOM element manipulation by closely resembling HTML syntax. JSX facilitates the creation of reusable UI building blocks, defined as JavaScript functions
2 min read
How to create a Read More component in ReactJS?
Creating a Read More component in React JS refers to hiding and displaying the text content on the page. It can be achieved by setting the state variable and display the content accordingly. PrerequisitesNode.JS and npmReact JSReact JS useState() hookApproachTo create a Read More component in React
3 min read
How to create components in ReactJS ?
Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read
How to update the State of a component in ReactJS ?
To display the latest or updated information, and content on the UI after any User interaction or data fetch from the API, we have to update the state of the component. This change in the state makes the UI re-render with the updated content. Prerequisites: NPM & Node.jsReact JSReact StateReact
3 min read
How to add Stateful component without constructor class in React?
Generally, we set the initial state of the component inside the constructor class and change the state using the setState method. In React basically, we write HTML-looking code called JSX. JSX is not a valid JavaScript code but to make the developer's life easier BABEL takes all the responsibility t
2 min read
How to use HOCs to reuse Component Logic in React ?
In React, making reusable components keeps your code neat. Higher-order components (HOCs) are a smart way to bundle and reuse component logic. HOCs are like magic functions that take a component and give you back an upgraded version with extra powers or information. HOCs can be implemented in a few
4 min read
How to make Reusable React Components ?
ReactJS is a JavaScript library used to build single-page applications (SPAs). React makes it easy to create an interactive user interface by using a component-based approach. In this article, we will learn about Reusable React Components with examples. Table of Content What are Reusable React Compo
4 min read
How to create Switch in ReactJS?
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
2 min read