Home > Web Front-end > JS Tutorial > Create a Toggle Switch in React as a Reusable Component

Create a Toggle Switch in React as a Reusable Component

William Shakespeare
Release: 2025-02-08 10:17:10
Original
1044 people have browsed it

Create a Toggle Switch in React as a Reusable Component

In this article, we’ll create an iOS-inspired toggle React component. This will be a small, self-contained component that you can reuse in future projects. As we go, we’ll also build a simple demo React app that uses our custom toggle switch component.

Although we could use third-party libraries for this, building the component from scratch allows us to better understand how our code works and allows us to customize our component completely.

The checkbox is traditionally used for collecting binary data, such as yes or no, true or false, enable or disable, on or off, etc. Although some modern interface designs avoid form fields when creating toggle switches, I’ll stick with them here due to their greater accessibility.

Here’s a screenshot of the component we’ll be building:

Create a Toggle Switch in React as a Reusable Component

Key Takeaways

  • Use Create React App to quickly set up a new React application and build a reusable iOS-inspired toggle switch component.
  • Leverage SCSS for styling the toggle switch, ensuring it’s visually appealing and aligns with modern design standards.
  • Implement the toggle switch as a controlled component in React, using props to manage its state and behavior dynamically.
  • Enhance accessibility by making the toggle switch keyboard accessible and using appropriate ARIA attributes.
  • Utilize PropTypes for type-checking within the component to ensure that the required data is passed and is of the correct type.
  • Explore advanced theming and responsiveness by incorporating SCSS variables and media queries to adapt the toggle switch’s appearance on different devices.

Step 1 – Creating the React App

Let’s use Create React App to quickly get a toggle switch React component up and running. If you’re unfamiliar with Create React App, check out our getting started guide.

create-react-app toggleswitch
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Once everything has been installed, change into the newly created directory and start the server with yarn start (or npm start if you prefer). This will start the development server at http://localhost:3000.

Next, create a ToggleSwitch directory in the src directory. This is where we will make our component:

mkdir src/ToggleSwitch
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

In this directory, make two files: ToggleSwitch.js and ToggleSwitch.scss:

touch ToggleSwitch.js ToggleSwitch.scss
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Finally, alter App.js as follows:

import React from 'react';
import ToggleSwitch from './ToggleSwitch/ToggleSwitch'

function App() {
  return (
    <ToggleSwitch />
  );
}

export default App;
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Step 2 – The Markup

We can start by setting up a basic HTML checkbox input form element for our toggle React component with its necessary properties.

create-react-app toggleswitch
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Then, add an enclosing 

tag around it and a

The above is the detailed content of Create a Toggle Switch in React as a Reusable Component. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template