React Checkbox Tree
react-checkbox-tree is a React component designed to render a hierarchical tree of checkboxes. It provides a simple and elegant solution for implementing multi-select functionality with nested options. Currently stable at version 2.0.1, released in April 2026, the library recently underwent a major accessibility-focused overhaul in its 2.0.0 release. While its release cadence has historically varied, with a significant gap before the v2.x releases, the recent activity suggests ongoing development. Key differentiators include its controlled component architecture, support for customizable icons (including Font Awesome v4, v5, and v6), robust accessibility features, and strict enforcement of unique node values to optimize performance for internal state management.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'length') or visually unstyled/broken component display.
cause The essential CSS stylesheet for `react-checkbox-tree` has not been imported or is not correctly loaded into the application.fixAdd `import 'react-checkbox-tree/lib/react-checkbox-tree.css';` to your application's entry file (e.g., `index.js`/`index.tsx`) or the React component where `CheckboxTree` is being rendered. -
Error: Node object property 'value' must be unique. (or similar runtime error related to duplicate keys).
cause Two or more node objects within the `nodes` prop have identical `value` properties, violating the component's unique value requirement.fixInspect your `nodes` array for any duplicate `value` strings. Each node's `value` must be unique across the entire tree structure. -
TypeError: CheckboxTree is not a function or Element type is invalid: expected a string (for built-in components) or a class/function (for composite components).
cause The `CheckboxTree` component is being imported incorrectly, either using CommonJS `require()` in an ES module context or misinterpreting its default export.fixEnsure you are using `import CheckboxTree from 'react-checkbox-tree';`. If explicitly using CommonJS, `const CheckboxTree = require('react-checkbox-tree').default;` may be necessary. -
Clicking checkboxes or expand/collapse icons has no effect; the tree state appears frozen.
cause The `CheckboxTree` is a controlled component, but the `checked` or `expanded` props are not being updated by their respective `onCheck` or `onExpand` event handlers, preventing state changes from being reflected.fixVerify that your `onCheck` and `onExpand` callback functions are correctly updating the state variables (e.g., using `setChecked` and `setExpanded` from `useState`) that are then passed back into the `checked` and `expanded` props.
Warnings
- breaking Version 2.0.0 introduced significant breaking changes related to accessibility. The clickable label's ARIA `role` was changed from `link` to `button`, and the pseudo-checkbox is now hidden from the accessibility tree to improve screen reader compatibility.
- breaking In version 2.0.0 and later, the `id` property on the `CheckboxTree` component will no longer automatically generate a random UUID if left empty. If you rely on a unique `id` for DOM manipulation or testing, it must now be explicitly provided.
- gotcha The component relies on Font Awesome CSS (v4, v5, or v6) to be loaded in your application for its default icons to render correctly. Without it, you will observe missing or broken icons.
- gotcha react-checkbox-tree is a controlled component. Its `checked` and `expanded` props must be managed externally by your React component's state. Failure to update these states via the `onCheck` and `onExpand` handlers will result in a non-interactive tree.
- gotcha All node objects within the `nodes` array passed to the component must have a unique `value` property. Duplicate values across any level of the tree will lead to unexpected behavior and can cause errors, as the component relies on `value` for internal state serialization.
Install
-
npm install react-checkbox-tree -
yarn add react-checkbox-tree -
pnpm add react-checkbox-tree
Imports
- CheckboxTree
const CheckboxTree = require('react-checkbox-tree');import CheckboxTree from 'react-checkbox-tree';
- CSS Styles
import 'react-checkbox-tree/lib/react-checkbox-tree.css';
- Node
import type { Node } from 'react-checkbox-tree'; - OnCheckChangeEventHandler
import type { OnCheckChangeEventHandler } from 'react-checkbox-tree';
Quickstart
import React, { useState } from 'react';
import CheckboxTree from 'react-checkbox-tree';
import 'react-checkbox-tree/lib/react-checkbox-tree.css';
const nodes = [
{
value: 'grand-parent',
label: 'Grand Parent',
children: [
{ value: 'parent-1', label: 'Parent 1',
children: [
{ value: 'child-1a', label: 'Child 1a' },
{ value: 'child-1b', label: 'Child 1b' },
],
},
{ value: 'parent-2', label: 'Parent 2' },
],
},
{ value: 'single-node', label: 'A Standalone Node' }
];
function MyCheckboxTreeWidget() {
// State for checked nodes, initialized as an empty array
const [checked, setChecked] = useState<string[]>([]);
// State for expanded nodes, initialized as an empty array
const [expanded, setExpanded] = useState<string[]>([]);
return (
<div style={{ padding: '20px', maxWidth: '400px', border: '1px solid #eee', borderRadius: '5px' }}>
<h2>Hierarchical Selection</h2>
<p>Selected items: <strong>{checked.length > 0 ? checked.join(', ') : 'None'}</strong></p>
<CheckboxTree
nodes={nodes}
checked={checked}
expanded={expanded}
onCheck={(checkedList) => setChecked(checkedList)}
onExpand={(expandedList) => setExpanded(expandedList)}
showExpandAll={true}
showNodeIcon={false}
/>
</div>
);
}
export default MyCheckboxTreeWidget;
// To use this in your App.tsx or App.jsx:
// import MyCheckboxTreeWidget from './MyCheckboxTreeWidget';
// function App() {
// return <MyCheckboxTreeWidget />;
// }