React Tree Select Component
raw JSON →rc-tree-select is a highly customizable React UI component designed for selecting single or multiple items from a hierarchical tree structure. As part of the `react-component` ecosystem, it prioritizes unstyled, highly performant, and accessible base functionality, giving developers full control over styling and integration into custom design systems. The current stable version is `5.27.0`. While it maintains a consistent release cadence, the project has also seen the introduction of a scoped package, `@rc-component/tree-select` (currently around v1.x), which represents an ongoing evolution of the component and may introduce new features or a migration path for future consumers. Its primary differentiator is its "headless" nature, providing foundational logic without imposing visual styles, making it extremely flexible for diverse application needs.
Common errors
error Cannot resolve module 'rc-tree-select/assets/index.less' (or '.css') ↓
import 'rc-tree-select/assets/index.less'; (or index.css) to your main application entry point or the component where TreeSelect is used. For Vite, you might need specific alias configurations if encountering resolution issues. error TypeError: Cannot read properties of undefined (reading 'map') (or similar errors related to data processing) ↓
treeData array conforms to the expected structure (an array of objects with value, label, and optional children). Ensure the data is not undefined or null before passing it to the TreeSelect component. error Error: `value` prop on `TreeSelect` did not match a previous value. A common cause is when a consumer passes an inline array or object as a `value` prop. Consider providing a stable value. ↓
value prop for TreeSelect (and other controlled props like expandedKeys) is derived from a stable state variable (e.g., using useState or useReducer) and is not being recreated as a new object/array instance on every render. Use JSON.stringify() or a stable reference for comparing complex value types. error TreeSelect dropdown does not open or appears unstyled/misplaced. ↓
import 'rc-tree-select/assets/index.less'; (or .css) is correctly applied. Check for z-index conflicts, making sure the TreeSelect dropdown has a higher z-index than surrounding elements. Inspect the rendered DOM for any unexpected styles overriding rc-tree-select's dropdown styles. Adjust dropdownStyle for positioning if necessary. Warnings
breaking Major version upgrades (e.g., v4 to v5) in `rc-tree-select` or related `rc-component` libraries frequently introduce breaking API changes, particularly around prop definitions, event handler signatures, or internal data structures. Always review the specific changelog for the version you are upgrading to. ↓
gotcha `rc-tree-select` is an unstyled component. It provides the core logic and structure but does not include any default visual styling beyond minimal functional CSS. Developers must import `rc-tree-select/assets/index.css` (or `.less` if using a preprocessor) and then apply their own custom CSS or integrate it into a UI framework's styling system to make it visually presentable.
gotcha Performance can degrade significantly with very large datasets (e.g., thousands of tree nodes) if not properly managed. Avoid rendering deeply nested or excessively numerous nodes simultaneously. Consider using features like `treeDefaultExpandAll={false}`, virtualization, or server-side data loading/filtering for optimal performance. Using `treeCheckStrictly` can also impact performance in `checkable` mode.
deprecated While `TreeNode` components can be passed as direct children to `TreeSelect`, the documentation often recommends using the `treeData` prop instead. Using `treeData` typically offers better performance, easier data management, and cleaner code, especially for dynamic or large tree structures. ↓
gotcha There can be confusion between `rc-tree-select` and the newer `@rc-component/tree-select` scoped package. While `rc-tree-select` (v5.x) is the package requested, the `react-component` organization actively develops `@rc-component/tree-select` (v1.x) which may reflect future evolutions or different API considerations. Always verify the package name and version you intend to use. ↓
Install
npm install rc-tree-select yarn add rc-tree-select pnpm add rc-tree-select Imports
- TreeSelect wrong
import TreeSelect from 'rc-tree-select';correctimport { TreeSelect } from 'rc-tree-select'; - TreeNode wrong
import TreeNode from 'rc-tree-select/lib/TreeNode';correctimport { TreeSelect, TreeNode } from 'rc-tree-select'; - TreeSelectProps wrong
import { TreeSelectProps } from 'rc-tree-select';correctimport type { TreeSelectProps } from 'rc-tree-select';
Quickstart
import React, { useState } from 'react';
import { TreeSelect } from 'rc-tree-select';
import 'rc-tree-select/assets/index.less'; // Or index.css for basic styles, though custom styling is common.
interface TreeDataItem {
value: string;
label: string;
children?: TreeDataItem[];
}
const treeData: TreeDataItem[] = [
{
value: 'parent 1',
label: 'Parent 1',
children: [
{
value: 'parent 1-0',
label: 'Parent 1-0',
children: [
{ value: 'leaf 1', label: 'Leaf 1' },
{ value: 'leaf 2', label: 'Leaf 2' },
],
},
{ value: 'parent 1-1', label: 'Parent 1-1' },
],
},
{ value: 'parent 2', label: 'Parent 2' },
];
const MyTreeSelectComponent: React.FC = () => {
const [value, setValue] = useState<string | string[] | undefined>(undefined);
const onChange = (newValue: string | string[] | undefined) => {
console.log('Selected:', newValue);
setValue(newValue);
};
return (
<div style={{ width: 300, margin: '50px auto' }}>
<h3>Basic TreeSelect Example</h3>
<TreeSelect
style={{ width: '100%' }}
value={value}
treeData={treeData}
placeholder="Please select"
onChange={onChange}
allowClear
showSearch
treeDefaultExpandAll
/>
<p>Selected Value: {JSON.stringify(value)}</p>
</div>
);
};
export default MyTreeSelectComponent;