React D3 Tree

3.6.6 · active · verified Sun Apr 19

React D3 Tree is a JavaScript library providing a React component for visualizing hierarchical data as interactive tree graphs. It leverages D3.js's `tree` layout for efficient rendering and interactivity, making it suitable for representing structures like family trees, organizational charts, or file directories. The current stable version is 3.6.6. The project maintains an active release cadence with frequent minor updates and bug fixes. Key differentiators include its declarative React interface for D3-based trees, built-in zooming, panning, and node collapsing, and strong TypeScript support, enabling developers to integrate complex visualizations with minimal setup.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to render a basic organizational chart using the Tree component, providing data, setting orientation, initial translation, and path rendering style.

import React from 'react';
import Tree from 'react-d3-tree';

interface OrgChartNode {
  name: string;
  attributes?: {
    department?: string;
    [key: string]: any;
  };
  children?: OrgChartNode[];
}

const orgChartData: OrgChartNode = {
  name: 'CEO',
  children: [
    {
      name: 'Manager',
      attributes: { department: 'Production' },
      children: [
        {
          name: 'Foreman',
          attributes: { department: 'Fabrication' },
          children: [{ name: 'Worker' }]
        },
        {
          name: 'Foreman',
          attributes: { department: 'Assembly' },
          children: [{ name: 'Worker' }]
        }
      ]
    }
  ]
};

export default function OrgChartTree() {
  return (
    <div id="treeWrapper" style={{ width: '100%', height: '500px', border: '1px solid #ccc' }}>
      <Tree 
        data={orgChartData} 
        orientation="vertical"
        translate={{ x: 50, y: 100 }} 
        nodeSize={{ x: 100, y: 100 }}
        separation={{ siblings: 1.5, nonSiblings: 1.5 }}
        pathFunc="step"
      />
    </div>
  );
}

view raw JSON →