Skip to content

Getting Started

Terminal window
npm install react-simple-tree-menu

React is the only peer dependency. The library works on React 16.14 and up.

import TreeMenu from 'react-simple-tree-menu';
import 'react-simple-tree-menu/styles';
const data = {
fruit: {
label: 'Fruits',
nodes: {
apple: { label: 'Apple' },
banana: { label: 'Banana' },
},
},
vegetable: {
label: 'Vegetables',
nodes: {
carrot: { label: 'Carrot' },
},
},
};
export default function App() {
return <TreeMenu data={data} />;
}

data can be either an object (recommended for readability) or an array (recommended when order matters and you’re iterating from a DB/API result).

const data = {
fruit: {
label: 'Fruits',
nodes: {
apple: { label: 'Apple' },
},
},
};

Keys become the node key. Iteration order follows the object’s property order.

const data = [
{
key: 'fruit',
label: 'Fruits',
nodes: [{ key: 'apple', label: 'Apple' }],
},
];

Every node must supply a unique key within its sibling group.

Prefer your own styles? Pass classNames and don’t import react-simple-tree-menu/styles.

<TreeMenu
data={data}
classNames={{
item: 'my-item',
active: 'my-item-active',
focused: 'my-item-focused',
search: 'my-search',
}}
/>

See the Theming guide for the full list of slots.