A fully customizable, recursive tree component for React with support for custom parent and child components.
Check out the interactive demo to see the component in action with various configurations and examples!
- 🎨 Fully Customizable - Bring your own components for parents and children
- 🔄 Recursive Rendering - Handles deeply nested tree structures
- ⚡ Lightweight - Minimal dependencies
- 🎯 TypeScript Ready - Written with modern React patterns
- 📦 Easy to Use - Simple API with sensible defaults
- 🎭 Flexible Styling - Control margins and layout
- 🔌 Event Handling - Built-in click handlers for child nodes
npm install react-custom-treeor
yarn add react-custom-treeimport React from 'react';
import Tree from 'react-custom-tree';
const data = [
{
id: 1,
name: 'Parent 1',
child: [
{
id: 2,
name: 'Child 1'
},
{
id: 3,
name: 'Child 2'
}
]
}
];
function App() {
return (
<Tree
data={data}
onChidClick={(child) => console.log('Clicked:', child)}
/>
);
}
export default App;| Prop | Type | Default | Required | Description |
|---|---|---|---|---|
data |
Array |
[] |
✅ | Array of tree node objects |
onChidClick |
Function |
undefined |
❌ | Callback fired when a child node is clicked |
childComponent |
Component |
Built-in | ❌ | Custom component for rendering child nodes |
parentComponent |
Component |
Built-in | ❌ | Custom component for rendering parent nodes |
isDefaultOpen |
Boolean |
false |
❌ | Whether parent nodes are expanded by default |
noLeftMargin |
Boolean |
false |
❌ | Remove left margin from tree nodes |
Each node in the data array should follow this structure:
{
id: number | string, // Unique identifier (required)
name: string, // Display name (required)
child: Array, // Array of child nodes (optional)
// ... any other custom properties
}import React from 'react';
import Tree from 'react-custom-tree';
import data from './data.json';
function BasicExample() {
return (
<Tree
data={data}
onChidClick={(child) => console.log(child)}
/>
);
}function DefaultOpenExample() {
return (
<Tree
data={data}
isDefaultOpen={true}
onChidClick={(child) => console.log(child)}
/>
);
}import React from 'react';
import Tree from 'react-custom-tree';
import data from './data.json';
// Custom child component
const CustomChild = (props) => (
<div className="custom-child">
📄 {props.name}
</div>
);
// Custom parent component
const CustomParent = (props) => (
<div className="custom-parent">
<span className="icon">
{props.open ? '📂' : '📁'}
</span>
<span className="name">{props.name}</span>
</div>
);
function CustomExample() {
return (
<Tree
data={data}
onChidClick={(child) => console.log(child)}
parentComponent={CustomParent}
childComponent={CustomChild}
/>
);
}const CustomParent = (props) => (
<div className="custom-parent">
<span className="icon">
{props.open ? (
<i className="fa fa-caret-down" aria-hidden="true"></i>
) : (
<i className="fa fa-caret-right" aria-hidden="true"></i>
)}
</span>
{props.name}
</div>
);
function FontAwesomeExample() {
return (
<Tree
data={data}
parentComponent={CustomParent}
onChidClick={(child) => console.log(child)}
/>
);
}function NoMarginExample() {
return (
<Tree
data={data}
noLeftMargin={true}
onChidClick={(child) => console.log(child)}
/>
);
}The component comes with minimal default styles. You can override them by targeting these CSS classes:
.tree-margin-left-15 {
margin-left: 15px;
}
.tree-parent-component {
cursor: pointer;
padding: 5px;
}
.tree-child-component {
cursor: pointer;
padding: 5px;
}[
{
"id": 1,
"name": "Parent 1",
"child": [
{
"id": 2,
"name": "Parent 1.1",
"child": [
{
"id": 3,
"name": "Child 1"
}
]
},
{
"id": 4,
"name": "Parent 1.2",
"child": [
{
"id": 5,
"name": "Child 2"
},
{
"id": 6,
"name": "Child 3"
}
]
}
]
},
{
"id": 7,
"name": "Parent 2",
"child": []
}
]Check out the live demo to see the component in action with various configurations.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the ISC License.
Sojin Antony
- GitHub: @sojinantony01
- npm: react-custom-tree
Special thanks to Viswanath Lekshmanan for contributions and support.
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)
- React >= 16.8.0 (hooks support required)
- React-DOM >= 16.8.0
Note: While the package works with React 16.8.0+, we recommend using React 18+ for optimal performance and latest features.
- ✨ Modernized with React functional components and hooks
- 🔄 Maintained backward compatibility with React 16.8.0+
- 📚 Enhanced documentation with comprehensive examples
- 🎨 Improved demo with modern UI and more examples
- 🚀 Added GitHub Actions for automated deployment
- 🎯 Better TypeScript support and modern patterns
- Initial stable release
- Basic tree functionality
- Custom component support
Made with ❤️ by Sojin Antony