Direactree

Large trees

Load folder contents only when they are opened, and render only the rows in view, so a tree with tens of thousands of nodes stays fast.

Lazy loading

Give a folder hasChildren: true and leave children out. The first time it opens, onLoadChildren runs and a loading row appears under it. If the Promise rejects, the row shows the error and a Retry button, and onActionError is called with "load".

useDireactree().loadChildren runs your loader and stores the result:

const tree = useDireactree([
  { id: "bucket", name: "s3://assets", type: "folder", hasChildren: true },
]);

<Direactree
  structure={tree.structure}
  onLoadChildren={(node) => tree.loadChildren(node, (folder) => api.listFolder(folder.id))}
/>
TRY ITLazy loading

Open a folder. The first open calls onLoadChildren, shows a loading row, and stores the result with tree.loadChildren. Turn on 'fail next request' to see the error row and Retry.

requests
Nothing yet. Use the tree.

Without the hook, put the loaded children into structure yourself and return the Promise from onLoadChildren, so the tree knows when loading ends:

onLoadChildren={async (node) => {
  const children = await api.listFolder(node.id);
  setStructure((current) => setChildren(current, node.id, children));
}}

Each folder is requested once. A folder that loaded an empty list stays empty. Loading follows the open state, so isAllExpanded, expandedIds, and revealNode can start loads as well.

Virtualization

<Direactree structure={structure} virtualized={{ height: 480 }} />

Only the rows in view, plus a few above and below, are in the page. Search, keyboard navigation, revealNode, drag and drop, editing, lazy loading, and the context menu all keep working.

TRY ITVirtualization

Use the arrow keys, Home, End, or the buttons. The counter shows how many rows are in the page right now. Without virtualized, the larger trees are left off so the page stays responsive.

  • monorepo
  • package-000
  • module-000.ts
  • module-001.ts
  • module-002.ts
  • module-003.ts
  • module-004.ts
  • module-005.ts
  • module-006.ts
  • module-007.ts
  • module-008.ts
  • module-009.ts
  • module-010.ts
  • module-011.ts
  • module-012.ts
  • module-013.ts
  • module-014.ts
  • module-015.ts
  • module-016.ts
  • module-017.ts
  • module-018.ts
live numbers
{
  "nodesInTree": 10001,
  "rowsInDom": 0,
  "virtualized": true
}
OptionDefaultUse
height360pxHeight of the scrolling area. A number means pixels. The CSS token --direactree-viewport-height does the same.
rowHeightThe density row height (28 or 22)Height of one row in pixels. It also sets --direactree-row-height, so CSS and the window math agree.
overscan8Extra rows rendered above and below the visible area.

virtualized={true} uses all the defaults. The scrolling area needs a fixed height, from height or CSS; otherwise it grows with the content and every row is rendered again.

If you change --direactree-row-height in CSS and leave rowHeight out, the tree reads the CSS value. The create and rename editor is taller than a row; it is measured when it opens.