React Switch Component

7.1.0 · active · verified Sun Apr 19

react-switch is a lightweight, customizable, and accessible toggle-switch component for React applications. Currently at version 7.1.0, it provides a draggable switch with sensible default styling that uses inline styles, eliminating the need for separate CSS imports. Key differentiators include its small bundle size (<2.5 kB gzipped), built-in accessibility features (with guidance on proper label usage), and high customizability for visual appearance. The library is actively maintained, though a specific release cadence isn't published. It supports a wide range of React versions, from 15.3.0 up to 19.0.0, as specified in its peer dependencies, making it compatible with both older and very recent React projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic controlled `Switch` component within a `label` for accessibility, handling state changes and customizable appearance.

import React, { Component } from "react";
import Switch from "react-switch";

class SwitchExample extends Component {
  constructor(props) {
    super(props);
    this.state = { checked: false };
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(checked) {
    this.setState({ checked });
  }

  render() {
    return (
      <label htmlFor="my-switch">
        <span>Toggle Feature</span>
        <Switch
          id="my-switch"
          onChange={this.handleChange}
          checked={this.state.checked}
          offColor="#ccc"
          onColor="#88b"
          height={24}
          width={48}
          handleDiameter={20}
          aria-label="Toggle feature on or off"
        />
      </label>
    );
  }
}

export default SwitchExample;

view raw JSON →