
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
@henliwu1491/react-tree
Advanced tools
[](https://www.npmjs.com/package/@henliwu1491/react-tree) [ {
const [selectedId] = React.useState(["12"]);
const [expandedId] = React.useState(["2", "21", "221"]);
return <TreeView initialState={{ selectedId, expandedId }} data={data} />;
}
import { TreeView } from "@henliwu1491/react-tree";
export default function Tree() {
const [selectedId] = React.useState(["12"]);
const [expandedId] = React.useState(["2", "21", "221"]);
return (
<TreeView
initialState={{ selectedId, expandedId }}
data={data}
getLabel={(item) => {
if (item.type === "leaf") {
return (
<div className="flex">
<div>Leaf: {item.label}</div>
</div>
);
}
return item.label;
}}
/>
);
}
import { TreeView } from "@henliwu1491/react-tree";
export default function Tree() {
const [selectedId] = React.useState(["12"]);
const [expandedId] = React.useState(["2", "21", "221"]);
return (
<TreeView
initialState={{ selectedId, expandedId }}
data={data}
icon={{
expand: "▲",
collapse: "▼",
leaf: "🌱",
checked: "☑",
unchecked: "☐",
indeterminate: "-", // Or <span>your custom React component</span>
}}
/>
);
}
export default function Tree() {
const [selectedId, setSelectedId] = React.useState(["12"]);
const [expandedId, setExpandedId] = React.useState(["2", "21", "221"]);
return (
<TreeView
value={{ selectedId, expandedId }}
data={data}
onExpand={(item) => {
setExpandedId((prev) =>
prev.length === 0
? [item.value]
: prev.indexOf(item.value) === -1
? [...prev, item.value]
: prev.filter((id) => id !== item.value),
);
}}
onSelect={(item) => {
setSelectedId((prev) =>
prev.length === 0
? [item.value]
: prev.indexOf(item.value) === -1
? [...prev, item.value]
: prev.filter((id) => id !== item.value),
);
}}
/>
);
}
Below are the available configuration options for the hook:
| Name | Type | Description | Default |
|---|---|---|---|
initialState | TreeInitialState | Optional. | |
data | TreeRawData[] | Required. Your raw tree structure data. (must contain id, label and value key) | |
onExpand | function | Optional. Callback function you can get node item from the parameter. | |
onSelect | function | Optional. Callback function you can get node item from the parameter. | |
leafKey | string | Optional. Customized leaf node. | 'leaf' |
| Name | Type | Description | Default |
|---|---|---|---|
expandedId | string[] | Array of expanded node IDs | [] |
selectedId | string[] | Array of selected node IDs | [] |
data | TreeRawData[] | Processed tree data structure | [] |
onExpand | function | Callback when node is expanded/collapsed | - |
onSelect | function | Callback when node is selected/deselected | - |
setExpand | (string) => void | Function to expand/collapse a node | - |
checkNodeAndChildren | (string) => void | Select a node and all its children | - |
checkSingleNode | (string) => void | Select only the specified node | - |
setInitialState | (TreeInitialState) => void | Set the initial expanded and selected state | - |
<TreeView /> PropsBelow are the available configuration options for the component:
| Name | Type | Description | Default |
|---|---|---|---|
initialState | TreeInitialState | Optional. | |
data | TreeRawData[] | Required. Your raw tree structure data. (must contain id, label and value key) | |
onExpand | function | Optional. Callback function you can get node item from the parameter. | |
onSelect | function | Optional. Callback function you can get node item from the parameter. | |
value | TreeInitialState | Optional. Control your own state. | |
icon | IconConfig | Optional. Provide your custom icon, React.ReactNode only. | |
getLabel | function | Optional. Your own label render function. | |
leafKey | string | Optional. Customized leaf node. | 'leaf' |
export type TreeInitialState = {
expandedId: string[];
selectedId: string[];
};
id, label, value are required. And if nested data is provided, children is also required.
export type TreeRawData = {
id: string;
label: string;
value: string;
children?: TreeRawData[];
[key: string]: any;
};
| Name | Type | Description | Default |
|---|---|---|---|
getLeafNodes | function | (data: TreeData[], leafkey?: string) => TreeData[] | |
getNormalizedNodes | function | (data: TreeRawData[], level = 0) => TreeRawData[] | |
getFlattenNodes | function | (data: TreeRawData[]) => TreeRawData[] | |
getExpandedNodes | function | (nodes: TreeData[], expandedId: TreeData['value'][]) => TreeData[] | |
getSelectedNodes | function | (nodes: TreeData[], selectedId: TreeData['value'][]) => TreeData[] | |
getSelectedIdWithChildren | function | (nodes: TreeData[], selectedIds: TreeData['value'][], checkId: string, set: Set<string or number> = new Set(selectedIds)) => TreeData[] |
We welcome contributions! If you find a bug or have an idea for improvement, please open an issue or submit a pull request on Github.
git checkout -b new-feature)git commit -am 'Add feature')git push origin new-feature)💻 Henry Wu(吳亨利)
This project is licensed under the MIT License.
FAQs
[](https://www.npmjs.com/package/@henliwu1491/react-tree) [
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.