React Organizational Chart
React Organizational Chart is a JavaScript library designed for rendering interactive hierarchy trees within React applications. Its primary distinguishing feature is the flexibility to accept any React children as node labels, enabling highly customized and rich node designs, which is a common limitation in many alternative organizational chart libraries. The current stable version is 2.2.1. While the package maintains a notable level of weekly downloads, indicating its continued utility, its development cadence is slow. The latest version was published approximately three years ago, and according to recent analyses, there have been no new releases in the past twelve months, though some minor commits have occurred more recently. It is typically employed for visualizing hierarchical data structures such as corporate organizational charts or family trees, offering styling customization through props for lines and node padding.
Common errors
-
Error: Objects are not valid as a React child (found: object with keys {…}). If you meant to render a collection of children, use an array instead.cause Attempting to pass a plain JavaScript object directly as a child or as the `label` prop without rendering it as a React element.fixEnsure that the `label` prop (and any children) are valid React nodes (e.g., JSX elements, strings, numbers, or arrays of these) and not raw objects. Wrap objects in a rendering component if their content needs to be displayed. -
TypeError: Cannot read properties of undefined (reading 'label') OR `label` is required in `Tree` / `TreeNode` component.
cause The `label` prop for either `Tree` or `TreeNode` component was omitted or evaluated to `undefined` during rendering.fixAlways provide a valid React Node (e.g., `<div>Content</div>`, a string, or another React component) for the `label` prop on both `Tree` and `TreeNode` components. -
Module not found: Can't resolve 'react-organizational-chart' in 'path/to/your/component'
cause The package `react-organizational-chart` is not installed or the import path is incorrect.fixRun `npm install react-organizational-chart` or `yarn add react-organizational-chart`. Double-check the import statement for typos: `import { Tree, TreeNode } from 'react-organizational-chart';`
Warnings
- gotcha Both `Tree` and `TreeNode` components require a `label` prop. Omitting this prop will result in runtime errors as the component expects a React Node to render at each position in the chart.
- gotcha Ensure your project's `react` and `react-dom` versions satisfy the peer dependency requirement of `>= 16.12.0`. Mismatched peer dependencies can lead to unexpected behavior or compilation errors.
- gotcha The library primarily uses props for styling structural elements like line width, color, and border-radius. While flexible, extensive inline styling via props might lead to verbose component code or style conflicts in larger applications if not encapsulated or managed carefully.
- deprecated The project exhibits a slow maintenance cadence. The latest version (2.2.1) was published three years ago, and no new releases have occurred in the last twelve months. While the package remains functional, this could imply reduced support for new React features or unresolved issues.
Install
-
npm install react-organizational-chart -
yarn add react-organizational-chart -
pnpm add react-organizational-chart
Imports
- Tree
const Tree = require('react-organizational-chart').Tree;import { Tree } from 'react-organizational-chart'; - TreeNode
import Tree, { TreeNode } from 'react-organizational-chart';import { TreeNode } from 'react-organizational-chart'; - { Tree, TreeNode }
import * as OrgChart from 'react-organizational-chart'; const MyTree = OrgChart.Tree;
import { Tree, TreeNode } from 'react-organizational-chart';
Quickstart
import React from 'react';
import { Tree, TreeNode } from 'react-organizational-chart';
import styled from 'styled-components'; // Example for custom styling
const StyledNode = styled.div`
padding: 8px 12px;
border-radius: 8px;
display: inline-block;
border: 1px solid #ddd;
background-color: #f9f9f9;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
`;
const StyledTreeExample = () => (
<Tree
lineWidth={'3px'}
lineColor={'#a0a0a0'}
lineBorderRadius={'10px'}
nodePadding={'15px'}
label={<StyledNode>Root Node</StyledNode>}
>
<TreeNode label={<StyledNode>Child 1</StyledNode>}>
<TreeNode label={<StyledNode>Grand Child A</StyledNode>} />
</TreeNode>
<TreeNode label={<StyledNode>Child 2</StyledNode>}>
<TreeNode label={<StyledNode>Grand Child B</StyledNode>}>
<TreeNode label={<StyledNode>Great Grand Child 1</StyledNode>} />
<TreeNode label={<StyledNode>Great Grand Child 2</StyledNode>} />
</TreeNode>
</TreeNode>
<TreeNode label={<StyledNode>Child 3</StyledNode>}>
<TreeNode label={<StyledNode>Grand Child C</StyledNode>} />
<TreeNode label={<StyledNode>Grand Child D</StyledNode>} />
</TreeNode>
</Tree>
);
export default StyledTreeExample;