React Checkbox Component
rc-checkbox is a foundational, unstyled React UI component that provides a customizable checkbox input. It aims to offer a robust and accessible base for building more elaborate checkbox UIs. The current stable version is 3.5.0. While active development for this component has effectively transitioned to the `@rc-component/checkbox` namespace, `rc-checkbox` version 3.5.0 remains available on npm. Its release cadence was moderate, with a significant rewrite to TypeScript in v3.0.0, and it differentiates itself by offering a simple, highly composable core component without imposing specific styling frameworks. Users seeking the latest features and ongoing maintenance should consider migrating to the `@rc-component/checkbox` package.
Common errors
-
TypeError: (0 , _rc_checkbox__WEBPACK_IMPORTED_MODULE_0__.default) is not a function
cause Attempting to import `Checkbox` as a named export when it is a default export.fixChange your import statement from `import { Checkbox } from 'rc-checkbox';` to `import Checkbox from 'rc-checkbox';`. -
Property 'someProp' does not exist on type 'IntrinsicAttributes & CheckboxProps'.
cause Using a prop that either does not exist on `CheckboxProps` or was removed/renamed during the v3.0.0 TypeScript rewrite.fixConsult the `rc-checkbox` (or `@rc-component/checkbox`) API documentation to verify valid props. If you're on v3.0.0+, ensure your code adheres to the new TypeScript interfaces. -
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
cause This error often indicates that the component `Checkbox` was not correctly imported or resolved, resulting in `undefined` being passed where a React component was expected.fixVerify that `import Checkbox from 'rc-checkbox';` is correctly placed and that `rc-checkbox` is properly installed in your `node_modules`.
Warnings
- breaking `rc-checkbox` v3.0.0 introduced a full rewrite in TypeScript. This may lead to changes in prop interfaces, internal component behavior, and requires TypeScript users to update their type imports and usage.
- breaking In `rc-checkbox` v3.5.0, a previously introduced accessibility improvement (feat: improve a11y #292) was reverted. This might impact users relying on those specific accessibility enhancements, potentially causing regressions in assistive technology support or keyboard navigation.
- gotcha The `rc-checkbox` package is functionally superseded by `@rc-component/checkbox`. While `rc-checkbox@3.5.0` is the last version under this name, all future development, breaking changes (e.g., DOM structure changes in `@rc-component/checkbox@2.0.0`), and new features will occur in the `@rc-component/checkbox` package. Users are strongly encouraged to migrate.
Install
-
npm install rc-checkbox -
yarn add rc-checkbox -
pnpm add rc-checkbox
Imports
- Checkbox
import { Checkbox } from 'rc-checkbox';import Checkbox from 'rc-checkbox';
- CheckboxProps
import type { CheckboxProps } from 'rc-checkbox'; - Checkbox (CommonJS)
const Checkbox = require('rc-checkbox');
Quickstart
import React, { useState } from 'react';
import Checkbox from 'rc-checkbox';
import 'rc-checkbox/assets/index.css'; // Don't forget to import the default styles if needed
const MyCheckboxComponent = () => {
const [isChecked, setIsChecked] = useState(false);
const handleChange = (e) => {
setIsChecked(e.target.checked);
console.log('Checkbox changed:', e.target.checked);
};
return (
<div>
<label>
<Checkbox
checked={isChecked}
onChange={handleChange}
name="myAwesomeCheckbox"
id="my-awesome-checkbox"
/>
<span>{isChecked ? 'Checked' : 'Unchecked'}</span>
</label>
<br />
<label>
<Checkbox defaultChecked disabled />
<span>Disabled and Checked</span>
</label>
<br />
<label>
<Checkbox disabled />
<span>Disabled and Unchecked</span>
</label>
</div>
);
};
export default MyCheckboxComponent;