Skip to main content

Creating a Reusable Toggle Switch Component in React

October 1, 2024
Creating a Reusable Toggle Switch Component in React

When developing React applications, reusable components can significantly enhance your code’s efficiency and readability. A toggle switch is a popular UI element, and in this article, we’ll demonstrate how to create a reusable toggle switch component that can be customised for various applications.

Why Use a Toggle Switch?

Toggle switches are a great way to allow users to make choices quickly. They can easily indicate two states – often ‘on’ and ‘off’ – and streamline user interactions with your application. Here are some key reasons to use a toggle switch:

  • Improves Usability: Users can make decisions fast.
  • Enhances Visual Appeal: A toggle switch can be more engaging than a checkbox.
  • Reduces Space: They are compact compared to other input elements.
Toggle switch

Setting Up the Component

To get started, ensure that you have a React environment set up. If you have not created a React app yet, you can do so by using Create React App:

npx create-react-app toggle-switch-example

Creating the Toggle Switch Component

Next, let’s create a functional toggle switch component. Here’s a basic implementation:

import React, { useState } from 'react';

const ToggleSwitch = ({ isToggled, onToggle }) => {
    return (
        <div className="toggle-switch" onClick={onToggle}>
            <div className={`switch ${isToggled ? 'on' : 'off'}`}>
                {isToggled ? 'ON' : 'OFF'}
            </div>
        </div>
    );
};

const App = () => {
    const [toggled, setToggled] = useState(false);

    return (
        <div>
            <h1>Reusable Toggle Switch</h1>
            <ToggleSwitch isToggled={toggled} onToggle={() => setToggled(!toggled)} />
        </div>
    );
};

export default App;

Styling the Component

To make your toggle switch visually appealing, you can add some CSS:


.toggle-switch {
    cursor: pointer;
    width: 60px;
    height: 30px;
    background-color: #ccc;
    border-radius: 15px;
    display: flex;
    align-items: center;
    justify-content: ${props => (props.isToggled ? 'flex-end' : 'flex-start')};
    padding: 5px;
    transition: background-color 0.2s ease;
}
.switch {
    width: 25px;
    height: 25px;
    border-radius: 50%;
    background-color: white;
    transition: transform 0.2s ease;
}
.on {
    transform: translateX(30px);
}
.off {
    transform: translateX(0);
}

Integrating Dynamic Functionality

To optimise the usability of your toggle switch, ensure that it performs actions tied to its state. This can be managed by passing in a function through props. As seen in our example, the onToggle prop updates the state based on the current toggle state.

Conclusion

Creating a reusable toggle switch component in React not only enhances your application’s interface but also promotes code reusability. You can further extend its functionality by incorporating animations or integrating it with external state management libraries.

With this guide, you’re now equipped to implement toggle switches in your next React project, providing a better experience for your users.

Leave a Reply

Your email address will not be published. Required fields are marked *

Request
a Consultation!

We pride ourselves on offering our clients a proactive and reliable, yet approachable, service to help them achieve their goals.