TypeScript icon, indicating that this package has built-in type declarations

3.4.0 • Public • Published


React Arborist

See the Demos

The tree view is ubiquitous in software applications. This library provides the React ecosystem with a complete solution to build the equivalent of a VSCode sidebar, Mac Finder, Windows Explorer, or Sketch/Figma layers panel.

Here is a Gmail sidebar clone built with react-arborist.

Gmail sidebar clone built with react-arborist


  • Drag and drop sorting
  • Open/close folders
  • Inline renaming
  • Virtualized rendering
  • Custom styling
  • Keyboard navigation
  • Aria attributes
  • Tree filtering
  • Selection synchronization
  • Callbacks (onScroll, onActivate, onSelect)
  • Controlled or uncontrolled trees


yarn add react-arborist
npm install react-arborist


Assume our data is this:

const data = [
  { id: "1", name: "Unread" },
  { id: "2", name: "Threads" },
    id: "3",
    name: "Chat Rooms",
    children: [
      { id: "c1", name: "General" },
      { id: "c2", name: "Random" },
      { id: "c3", name: "Open Source Projects" },
    id: "4",
    name: "Direct Messages",
    children: [
      { id: "d1", name: "Alice" },
      { id: "d2", name: "Bob" },
      { id: "d3", name: "Charlie" },

The Simplest Tree

Use all the defaults. The initialData prop makes the tree an uncontrolled component. Create, move, rename, and delete will be handled internally.

function App() {
  return <Tree initialData={data} />;



Customize the Appearance

We provide our own dimensions and our own Node component.

function App() {
  return (
      padding={25 /* sets both */}

function Node({ node, style, dragHandle }) {
  /* This node instance can do many things. See the API reference. */
  return (
    <div style={style} ref={dragHandle}>
      {node.isLeaf ? "🍁" : "🗀"}



Control the Tree data

Here we use the data prop to make the tree a controlled component. We must handle all the data modifications ourselves using the props below.

function App() {
  /* Handle the data modifications outside the tree component */
  const onCreate = ({ parentId, index, type }) => {};
  const onRename = ({ id, name }) => {};
  const onMove = ({ dragIds, parentId, index }) => {};
  const onDelete = ({ ids }) => {};

  return (

Tree Filtering

Providing a non-empty searchTerm will only show nodes that match. If a child matches, all its parents also match. Internal nodes are opened when filtering. You can provide your own searchMatch function, or use the default.

function App() {
  const term = useSearchTermString()
      (node, term) =>

Sync the Selection

It's common to open something elsewhere in the app, but have the tree reflect the new selection.

Passing an id to the selection prop will select and scroll to that node whenever that id changes.

function App() {
  const chatId = useCurrentChatId();

    Whenever the currentChatRoomId changes, 
    the tree will automatically select it and scroll to it. 

  return <Tree initialData={data} selection={chatId} />;

Use the Tree Api Instance

You can access the Tree Api in the parent component by giving a ref to the tree.

function App() {
  const treeRef = useRef();

  useEffect(() => {
    const tree = treeRef.current;
    /* See the Tree API reference for all you can do with it. */
  }, []);

  return <Tree initialData={data} ref={treeRef} />;

Data with Different Property Names

The idAccessor and childrenAccessor props allow you to specify the children and id fields in your data.

function App() {
  const data = [
      category: "Food",
      subCategories: [{ category: "Restaurants" }, { category: "Groceries" }],
  return (
      /* An accessor can provide a string property name */
      /* or a function with the data as the argument */
      childrenAccessor={(d) => d.subCategories}

Custom Rendering

Render every single piece of the tree yourself. See the API reference for the props passed to each renderer.

function App() {
  return (
      /* The outer most element in the list */
      /* The "ghost" element that follows the mouse as you drag */
      /* The line that shows where an element will be dropped */
      {/* The inner element that shows the indentation and data */}

API Reference

Tree Component Props

These are all the props you can pass to the Tree component.

interface TreeProps<T> {
  /* Data Options */
  data?: readonly T[];
  initialData?: readonly T[];

  /* Data Handlers */
  onCreate?: handlers.CreateHandler<T>;
  onMove?: handlers.MoveHandler<T>;
  onRename?: handlers.RenameHandler<T>;
  onDelete?: handlers.DeleteHandler<T>;

  /* Renderers*/
  children?: ElementType<renderers.NodeRendererProps<T>>;
  renderRow?: ElementType<renderers.RowRendererProps<T>>;
  renderDragPreview?: ElementType<renderers.DragPreviewProps>;
  renderCursor?: ElementType<renderers.CursorProps>;
  renderContainer?: ElementType<{}>;

  /* Sizes */
  rowHeight?: number;
  overscanCount?: number;
  width?: number | string;
  height?: number;
  indent?: number;
  paddingTop?: number;
  paddingBottom?: number;
  padding?: number;

  /* Config */
  childrenAccessor?: string | ((d: T) => T[] | null);
  idAccessor?: string | ((d: T) => string);
  openByDefault?: boolean;
  selectionFollowsFocus?: boolean;
  disableMultiSelection?: boolean;
  disableEdit?: string | boolean | BoolFunc<T>;
  disableDrag?: string | boolean | BoolFunc<T>;
    | string
    | boolean
    | ((args: {
        parentNode: NodeApi<T>;
        dragNodes: NodeApi<T>[];
        index: number;
      }) => boolean);

  /* Event Handlers */
  onActivate?: (node: NodeApi<T>) => void;
  onSelect?: (nodes: NodeApi<T>[]) => void;
  onScroll?: (props: ListOnScrollProps) => void;
  onToggle?: (id: string) => void;
  onFocus?: (node: NodeApi<T>) => void;

  /* Selection */
  selection?: string;

  /* Open State */
  initialOpenState?: OpenMap;

  /* Search */
  searchTerm?: string;
  searchMatch?: (node: NodeApi<T>, searchTerm: string) => boolean;

  /* Extra */
  className?: string | undefined;
  rowClassName?: string | undefined;

  dndRootElement?: globalThis.Node | null;
  onClick?: MouseEventHandler;
  onContextMenu?: MouseEventHandler;
  dndManager?: DragDropManager;

Row Component Props

The <RowRenderer> is responsible for attaching the drop ref, the row style (top, height) and the aria-attributes. The default should work fine for most use cases, but it can be replaced by your own component if you need. See the renderRow prop in the <Tree> component.

type RowRendererProps<T> = {
  node: NodeApi<T>;
  innerRef: (el: HTMLDivElement | null) => void;
  attrs: HTMLAttributes<any>;
  children: ReactElement;

Node Component Props

The <NodeRenderer> is responsible for attaching the drag ref, the node style (padding for indentation), the visual look of the node, the edit input of the node, and anything else you can dream up.

There is a default renderer, but it's only there as a placeholder to get started. You'll want to create your own component for this. It is passed as the <Tree> components only child.

export type NodeRendererProps<T> = {
  style: CSSProperties;
  node: NodeApi<T>;
  tree: TreeApi<T>;
  dragHandle?: (el: HTMLDivElement | null) => void;
  preview?: boolean;

DragPreview Component Props

The <DragPreview> is responsible for showing a "ghost" version of the node being dragged. The default is a semi-transparent version of the NodeRenderer and should work fine for most people. To customize it, pass your new component to the renderDragPreview prop.

type DragPreviewProps = {
  offset: XYCoord | null;
  mouse: XYCoord | null;
  id: string | null;
  dragIds: string[];
  isDragging: boolean;

Cursor Component Props

The <Cursor> is responsible for showing a line that indicates where the node will move to when it's dropped. The default is a blue line with circle on the left side. You may want to customize this. Pass your own component to the renderCursor prop.

export type CursorProps = {
  top: number;
  left: number;
  indent: number;

Node API Reference

State Properties

All these properties on the node instance return booleans related to the state of the node.


Returns true if this is the root node. The root node is added internally by react-arborist and not shown in the UI.


Returns true if the children property is not an array.


Returns true if the children property is an array.


Returns true if node is internal and in an open state.


Returns true if this node is currently being edited. Use this property in the NodeRenderer to render the rename form.


Returns true if node is selected.


Returns true if node is the first of a contiguous group of selected nodes. Useful for styling.


Returns true if node is the last of a contiguous group of selected nodes. Useful for styling.


Returns true if node is the only node selected in the tree.


Returns true if node is focused.


Returns true if node is being dragged.


Returns true if node is internal and the user is hovering a dragged node over it.


Returns an object with all the above properties as keys and boolean values. Useful for adding class names to an element with a library like clsx or classnames.

type NodeState = {
  isEditing: boolean;
  isDragging: boolean;
  isSelected: boolean;
  isSelectedStart: boolean;
  isSelectedEnd: boolean;
  isFocused: boolean;
  isOpen: boolean;
  isClosed: boolean;
  isLeaf: boolean;
  isInternal: boolean;
  willReceiveDrop: boolean;



Returns the node's index in relation to its siblings.

Returns the next visible node. The node directly under this node in the tree component. Returns null if none exist.


Returns the previous visible node. The node directly above this node in the tree component. Returns null if none exist.


Returns the next sibling in the data of this node. Returns null if none exist.

Selection Methods

Select only this node.


Deselect this node. Other nodes may still be selected.


Select this node while maintaining all other selections.


Deselect all nodes from the anchor node to the last selected node, the select all nodes from the anchor node to this node. The anchor changes to the focused node after calling select() or selectMulti().

Activation Methods


Runs the Tree props' onActivate callback passing in this node.


Focus this node.

Open/Close Methods

Opens the node if it is an internal node.


Closes the node if it is an internal node.


Toggles the open/closed state of the node if it is an internal node.


Opens all the parents of this node.


Moves this node into the editing state. Calling node.isEditing will return true.


Submits newName string to the onRename handler. Moves this node out of the editing state.


Moves this node out of the editing state without submitting a new name.

Event Handlers


Useful for using the standard selection methods when a node is clicked. If the meta key is down, call multiSelect(). If the shift key is down, call selectContiguous(). Otherwise, call select() and activate().

Tree API Reference

The tree api reference is stable across re-renders. It always has the most recent state and props.

Node Accessors

tree.get(id) : NodeApi | null

Get node by id from the visibleNodes array. : NodeApi | null

Get node by index from the visibleNodes array.

tree.visibleNodes : NodeApi[]

Returns an array of the visible nodes.

tree.firstNode : NodeApi | null

The first node in the visibleNodes array.

tree.lastNode : NodeApi | null

The last node in the visibleNodes array.

tree.focusedNode : NodeApi | null

The currently focused node.

tree.mostRecentNode : NodeApi | null

The most recently selected node.

tree.nextNode : NodeApi | null

The node directly after the focusedNode in the visibleNodes array.

tree.prevNode : NodeApi | null

The node directly before the focusedNode in the visibleNodes array.

Focus Methods

tree.hasFocus : boolean

Returns true if the the tree has focus somewhere within it.


Focus on the node with id.

tree.isFocused(id) : boolean

Check if the node with id is focused.


Move focus up one page.


Move focus down one page.

Selection Methods

tree.selectedIds : Set<string>

Returns a set of ids that are selected.

tree.selectedNodes : NodeApi[]

Returns an array of nodes that are selected.

tree.hasNoSelection : boolean

Returns true if nothing is selected in the tree.

tree.hasSingleSelection : boolean

Returns true if there is only one selection.

tree.hasMultipleSelections : boolean

Returns true if there is more than one selection.

tree.isSelected(id) : boolean

Returns true if the node with id is selected.

Select only the node with id.


Deselect the node with id.


Add to the selection the node with id.


Deselected nodes between the anchor and the last selected node, then select the nodes between the anchor and the node with id.


Deselect all nodes.


Select all nodes.


Open the node with id.


Close the node with id.


Toggle the open state of the node with id.


Open all parents of the node with id.


Open all siblings of the node with id.


Open all internal nodes.


Close all internal nodes.

tree.isOpen(id) : boolean

Returns true if the node with id is open.

Drag and Drop

tree.isDragging(id) : boolean

Returns true if the node with id is being dragged.

tree.willReceiveDrop(id) : boolean

Returns true if the node with id is internal and is under the dragged node.


tree.scrollTo(id, [align])

Scroll to the node with id. If this node is not visible, this method will open all its parents. The align argument can be "auto" | "smart" | "center" | "end" | "start".


tree.isEditing : boolean

Returns true if the tree is editing a node.

tree.isFiltered : boolean

Returns true if the searchTerm prop is not an empty string when trimmed.

tree.props : TreeProps

Returns all the props that were passed to the <Tree> component.

tree.root : NodeApi

Returns the root NodeApi instance. Its children are the Node representations of the data prop array.


James Kerr at Brim Data for the Zui desktop app.

Package Sidebar


npm i react-arborist

Weekly Downloads






Unpacked Size

343 kB

Total Files


Last publish


  • jkerr