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.
npm install react-switchVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic controlled `Switch` component within a `label` for accessibility, handling state changes and customizable appearance.
Ensure both `checked={this.state.someBoolean}` and `onChange={this.handleChange}` are passed to the `Switch` component, where `handleChange` updates the state tied to `checked`.Wrap the `Switch` component and its descriptive text in a `<label>` element, or explicitly link them using `htmlFor`/`id` or `aria-label`/`aria-labelledby`.
Verify that your `react` and `react-dom` versions satisfy the peer dependency requirements of `react-switch`. Update React or use an older `react-switch` version if necessary.
Provide a boolean value for the `checked` prop, typically from component state: `checked={this.state.isChecked}`.Add an `onChange` handler to the `Switch` component that updates the state controlling the `checked` prop: `onChange={this.handleChange}`.Wrap the `Switch` and its descriptive text within a `<label>` tag or use `htmlFor` and `id` attributes to link them, or `aria-label`/`aria-labelledby` props on the `Switch` itself.