Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-switch

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-switch

Draggable toggle-switch component for react

  • 3.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
192K
decreased by-24.4%
Maintainers
1
Weekly downloads
 
Created

What is react-switch?

The react-switch npm package is a lightweight and customizable switch component for React applications. It allows developers to easily integrate toggle switches into their user interfaces, providing a simple and intuitive way for users to switch between two states.

What are react-switch's main functionalities?

Basic Switch

This code demonstrates how to implement a basic switch using the react-switch package. The switch toggles between two states, and the state is managed using React's useState hook.

import React, { useState } from 'react';
import Switch from 'react-switch';

function App() {
  const [checked, setChecked] = useState(false);

  const handleChange = nextChecked => {
    setChecked(nextChecked);
  };

  return (
    <div>
      <Switch onChange={handleChange} checked={checked} />
    </div>
  );
}

export default App;

Custom Styling

This code sample shows how to customize the appearance of the switch. You can change the colors of the switch when it is on or off, as well as the handle colors.

import React, { useState } from 'react';
import Switch from 'react-switch';

function App() {
  const [checked, setChecked] = useState(false);

  const handleChange = nextChecked => {
    setChecked(nextChecked);
  };

  return (
    <div>
      <Switch
        onChange={handleChange}
        checked={checked}
        offColor="#888"
        onColor="#0f0"
        offHandleColor="#fff"
        onHandleColor="#000"
      />
    </div>
  );
}

export default App;

Disabled Switch

This example demonstrates how to disable the switch. When the switch is disabled, it cannot be toggled by the user.

import React, { useState } from 'react';
import Switch from 'react-switch';

function App() {
  const [checked, setChecked] = useState(false);

  const handleChange = nextChecked => {
    setChecked(nextChecked);
  };

  return (
    <div>
      <Switch onChange={handleChange} checked={checked} disabled={true} />
    </div>
  );
}

export default App;

Other packages similar to react-switch

Keywords

FAQs

Package last updated on 15 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc