React Switch UI Component
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
-
TS2307: Cannot find module 'rc-switch' or its corresponding type declarations.
cause The TypeScript compiler cannot locate the package's type definitions. This might happen if the package is not installed, or if you're using `@rc-component/switch` but still importing from `rc-switch`.fixEnsure `rc-switch` is correctly installed (`npm install rc-switch` or `yarn add rc-switch`). If you intended to use the newer package, install `@rc-component/switch` instead (`npm install @rc-component/switch`) and update your import statements accordingly. -
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
cause This error occurs when React hooks (like `useState`, `useEffect`) are used in an invalid context, such as outside a functional component or in an incompatible React version environment.fixEnsure your project's React version meets the peer dependency requirements (>=16.9.0). Verify that `rc-switch` is being used within a valid React functional component or class component render method, and that your build setup correctly handles React's rendering lifecycle. -
TypeError: Cannot read properties of undefined (reading 'setState')
cause This error typically indicates that `this` context is lost in an event handler within a class component, or that a state update is attempted on an unmounted component or an invalid object.fixIf using class components, ensure event handlers are bound correctly (e.g., using arrow functions or binding in the constructor). For functional components, always use the `useState` setter function provided by the hook, and avoid directly mutating state.
Warnings
- breaking The `rc-switch` package has been effectively renamed and superseded by `@rc-component/switch`. All new feature development and active maintenance are now focused on the new scoped package. While `rc-switch` v4.1.0 is stable, users are encouraged to migrate to `@rc-component/switch` for ongoing updates and support.
- breaking Version 4.0.0 introduced new props like `inner container` and `inner checked/unchecked` for enhanced customization. While not strictly breaking existing functionality, ensure your codebase is compatible if you were relying on internal DOM structure or specific prop behaviors from v3.
- gotcha Older versions of `rc-switch` (prior to v3.2.2) might exhibit compatibility issues or warnings when used with React 17 or newer versions.
Install
-
npm install rc-switch -
yarn add rc-switch -
pnpm add rc-switch
Imports
- Switch
const Switch = require('rc-switch');import Switch from 'rc-switch';
- SwitchProps
import { SwitchProps } from 'rc-switch';import type { SwitchProps } from 'rc-switch'; - Switch
import Switch, { SwitchProps } from 'rc-switch';
Quickstart
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;