React Tree Component (Legacy)
rc-tree is a foundational React UI component that provided a highly customizable and performant tree view. Its purpose was to display hierarchical data with features such as node expansion, selection, checking, dragging, and asynchronous data loading. The last stable version under the `rc-tree` package name is 5.13.1. Development on this package has ceased under this name, as it has been officially renamed and migrated to `@rc-component/tree` starting from version `@rc-component/tree@1.2.2`. While `rc-tree` offered robust capabilities like custom node rendering, virtualized lists for large datasets (in later versions before the rename), and broad browser support (including IE9+), users are strongly advised to migrate to the actively maintained `@rc-component/tree` for ongoing updates, bug fixes, and new features. The original `rc-tree` package itself does not have a defined release cadence anymore due to its renamed status.
Common errors
-
Module not found: Error: Can't resolve 'rc-tree'
cause The package `rc-tree` is not installed, or the import path is incorrect, or the package was uninstalled after migrating to the new name (`@rc-component/tree`).fixVerify `rc-tree` is listed in your `package.json` and installed. If you intended to use the newer version, install `@rc-component/tree` and update your imports accordingly. -
TypeError: (0, _rc_tree.default) is not a function
cause This typically occurs when trying to `require` or `import` the `Tree` component incorrectly. `rc-tree` uses a default export for the main component, but sometimes transpilation or incorrect module resolution can lead to issues.fixEnsure you are using `import Tree from 'rc-tree';` in ESM or `const Tree = require('rc-tree');` (or `const Tree = require('rc-tree').default;` if your bundler/loader requires it) in CJS. -
Tree component renders without any styling or with broken layout.
cause The essential CSS stylesheet for `rc-tree` has not been imported into the application.fixAdd `import 'rc-tree/assets/index.css';` to your main application file or the component where `rc-tree` is used. Ensure your build system is configured to process CSS imports.
Warnings
- breaking The `rc-tree` package has been renamed to `@rc-component/tree`. All future development, bug fixes, and new features will occur under the new package name. `rc-tree` (npm) is no longer actively maintained.
- gotcha The `rc-tree` package relies on specific CSS for its styling. If the `rc-tree/assets/index.css` file is not imported, the component will render unstyled, appearing broken or misaligned.
- deprecated Using `rc-tree` in modern JavaScript projects, especially those strictly enforcing ESM, might lead to compatibility issues. `rc-tree` was developed in an era when CommonJS was prevalent, and its default export might require specific handling in ESM-only contexts.
Install
-
npm install rc-tree -
yarn add rc-tree -
pnpm add rc-tree
Imports
- Tree
import { Tree } from 'rc-tree'; const Tree = require('rc-tree').default;import Tree from 'rc-tree';
- rc-tree/assets/index.css
import 'rc-tree/assets/index.css';
- TreeProps
import { TreeProps } from 'rc-tree';import type { TreeProps } from 'rc-tree';
Quickstart
import React, { useState } => from 'react';
import Tree from 'rc-tree';
import 'rc-tree/assets/index.css';
const treeData = [
{
key: '0-0',
title: 'Parent Node 1',
children: [
{ key: '0-0-0', title: 'Child Node 1-1' },
{ key: '0-0-1', title: 'Child Node 1-2' },
],
},
{
key: '0-1',
title: 'Parent Node 2',
children: [
{ key: '0-1-0', title: 'Child Node 2-1' },
{ key: '0-1-1', title: 'Child Node 2-2' },
],
},
];
const MyTreeComponent = () => {
const [expandedKeys, setExpandedKeys] = useState([]);
const [checkedKeys, setCheckedKeys] = useState([]);
const [selectedKeys, setSelectedKeys] = useState([]);
const onExpand = (keys) => {
console.log('onExpand', keys);
setExpandedKeys(keys);
};
const onCheck = (keys, info) => {
console.log('onCheck', keys, info);
setCheckedKeys(keys);
};
const onSelect = (keys, info) => {
console.log('onSelect', keys, info);
setSelectedKeys(keys);
};
return (
<div style={{ padding: '20px' }}>
<h2>Basic rc-tree Example</h2>
<Tree
checkable
defaultExpandAll
onExpand={onExpand}
expandedKeys={expandedKeys}
onCheck={onCheck}
checkedKeys={checkedKeys}
onSelect={onSelect}
selectedKeys={selectedKeys}
treeData={treeData}
/>
</div>
);
};
export default MyTreeComponent;