React Switch UI Component

4.1.0 · renamed · verified Sun Apr 19

rc-switch is an unstyled, highly customizable switch (toggle) UI component for React applications, providing fundamental functionality without imposing visual opinions. The package provided here, `rc-switch`, reached its last major release at version 4.1.0. Since then, development and new features have continued under a new scoped package name, `@rc-component/switch`, which currently stands at version 1.0.3. This library is part of the `react-component` ecosystem, known for delivering foundational, barebones UI elements designed for maximum stylistic flexibility. It emphasizes broad browser compatibility and a minimal bundle size, making it suitable for integration into custom design systems. Releases for the original `rc-switch` package have ceased, with all active development now occurring in its successor package, `@rc-component/switch`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of the Switch component, including state management for controlled usage and custom styling.

import Switch from 'rc-switch';
import React from 'react';

const MySwitchComponent = () => {
  const [checked, setChecked] = React.useState(false);

  const handleChange = (newChecked) => {
    console.log('Switch toggled:', newChecked);
    setChecked(newChecked);
  };

  return (
    <div>
      <p>My Custom Switch:</p>
      <Switch
        checked={checked}
        onChange={handleChange}
        checkedChildren="ON"
        unCheckedChildren="OFF"
        className="my-custom-switch"
        style={{ backgroundColor: checked ? '#1890ff' : '#bfbfbf' }}
      />
      <p>Current state: {checked ? 'Enabled' : 'Disabled'}</p>
    </div>
  );
};

export default MySwitchComponent;

view raw JSON →