Skip to main content
Version: 1.0.4

useTreeViewContext

@spartanfx/react v1.0.4


@spartanfx/react / useTreeViewContext

Function: useTreeViewContext()

useTreeViewContext<T>(): ITreeViewContextValue<T>

Hook to access TreeView context for external control and queries.

Must be used within a TreeView component or TreeViewProvider.

Provides programmatic access to:

  • Selection state (selectedId, setSelectedId)
  • Expansion state (expandedIds, toggleExpanded, expandAll, collapseAll)
  • Node queries (getNodeById, nodes, nodeMap)
  • Node manipulation (moveNode)

Type Parameters

T

T = any

Returns

ITreeViewContextValue<T>

Example

function TreeControls() {
const { expandAll, collapseAll, selectedId } = useTreeViewContext();

return (
<div>
<button onClick={expandAll}>Expand All</button>
<button onClick={collapseAll}>Collapse All</button>
{selectedId && <p>Selected: {selectedId}</p>}
</div>
);
}

<TreeView items={items} schema={schema}>
<TreeControls />
</TreeView>

Throws

Error if used outside of TreeViewProvider