Required Props
These props are required for ReactAppleTree to function properly. You must provide all three of these props when using the component.treeData
The array of tree items representing your hierarchical data structure.Each
TreeItem can contain the following properties:title: Primary label for the node (React.ReactNode)subtitle: Secondary label for the node (React.ReactNode)expanded: Whether children are visible (boolean, defaults to false)children: Array of child TreeItem nodesid: Optional unique identifier (string | number)
TypeScript Signature
Example
Custom Properties
You can extendTreeItem with custom properties using TypeScript generics:onChange
Callback function triggered whenever the tree data changes due to user interactions (dragging, dropping, expanding, etc.).Just like with React input elements, you must update your component’s state with the new tree data to see changes reflected in the UI.
TypeScript Signature
Parameters
treeData: The updated tree data structure after the change
Example
Advanced Example with Logging
When onChange is Called
- When a node is dragged and dropped to a new position
- When a node is moved to a different parent
- When nodes are reordered
- When nodes are added or removed programmatically
The
onChange callback is not called when nodes are expanded or collapsed. Use onVisibilityToggle for that.getNodeKey
Function that returns a unique key for each node. This key is used internally to identify nodes and generate path arrays in callbacks.The unique key helps ReactAppleTree track nodes efficiently and is crucial for callbacks to identify which node is being operated on.
TypeScript Signature
Parameters
data.node: The tree node objectdata.treeIndex: The index of the node in the flattened tree
Returns
A unique string or number identifying the nodeExample with ID Property
Example with Tree Index
If your nodes don’t have unique IDs, you can use the tree index:Example with Composite Key
Why It’s Important
The key returned bygetNodeKey is used in callback props to help you identify nodes:Best Practices
- Always use a stable, unique identifier from your data model (e.g., database ID)
- Avoid using array indices as keys for dynamic trees
- Ensure keys remain consistent across re-renders
- Keys should be unique across the entire tree, not just among siblings
Complete Example
Here’s a complete example using all three required props:Related Props
- See Callback Props for event handlers like
onMoveNodeandonVisibilityToggle - See Behavior Props for controlling tree behavior like
maxDepthandisVirtualized - See Styling Props for customizing the tree’s appearance