How to Create a Toggle Switch in ReactJS

share link

by vsasikalabe dot icon Updated: Feb 7, 2023

technology logo
technology logo

Solution Kit Solution Kit  

The React Switch Button component is a custom input-type checkbox component that allows you to do a toggle (on/off) action between checked and unchecked states. It also supports different sizes, labels, label positions, and UI customization.


Renders a boolean input. We can also use the following methods in ReactJS to use the Switch Component properly. Most of the time, the information you need to get from a user is boolean-like — for example, yes or no, true or false, enable or disable, on or off, etc. generally, the checkbox form component is used for getting these kinds of input.

We’ll need the following props to create react switch button:

  • id (required): this is the id that’s going to be passed to the checkbox input. The component will not be rendered without this id.
  • checked (required): this will execute the current state value, which will be a boolean value.
  • onChange (required): when the input’s onChange event handler is triggered this function will be called.
  • name (optional): this will be the label text of the checkbox input, but we generally won’t be using this.
  • small (optional): this is a boolean value, which renders the Toggle Switch in a small mode, where the text isn’t rendered.
  • optionLabels (optional): if you aren’t using the small version of the control, you might need to pass this to the Toggle Switch as an array of two values, which signify the text for True and False. An example would be Text={["Yes", "No"]}.
  • disabled (optional): By setting the disabled property as true the switch can be disabled.
  • The span tag is an inline container used to execute a part of a text, or a part of a document. The span tag is easily styled by CSS or facilitated with JavaScript using the class or id attribute.


Here is an example of how to create react switch button:

Preview of the output that you will get on running this code from your IDE.

Code:

In this solution we use the React library.

Instructions

Follow the steps carefully to get the output easily.

  1. Install the Node.js and React on your IDE(preferable Visual Studio Code).
  2. Create React Application using npx create-react-app foldername.
  3. cd foldername.
  4. Open the folder in IDE.
  5. Copy the code using "copy" button above and paste it in App.js file(remove the earlier code from App.js).
  6. Open the terminal from IDE.
  7. npm start to run the file.


I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.


I found this code snippet by searching for 'Create React Switch Button' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in VS Code 1.73.1 version.
  2. The solution is tested on Nodejs 16.14.2 version.
  3. react 18.2.0 version.


Using this solution, we are able to Create React Switch Button with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to Create React Switch Button.

Dependent Libraries

create-react-appby facebook

JavaScript doticonstar image 100082 doticonVersion:v5.0.1doticon
License: Permissive (MIT)

Set up a modern web app by running one command.

Support
    Quality
      Security
        License
          Reuse

            create-react-appby facebook

            JavaScript doticon star image 100082 doticonVersion:v5.0.1doticon License: Permissive (MIT)

            Set up a modern web app by running one command.
            Support
              Quality
                Security
                  License
                    Reuse

                      You can search for any dependent library on kandi like 'react'.

                      Support

                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page

                      See similar Kits and Libraries