useTreeState

Provides state management for tree-like components. Handles building a collection of items from props, item expanded state, and manages multiple selection state.

installyarn add @react-stately/tree
version3.2.4
usageimport {useTreeState} from '@react-stately/tree'

API#


useTreeState<T extends object>( (props: TreeProps<T> )): TreeState<T>

Interface#


Properties

NameTypeDescription
collectionCollection<Node<T>>A collection of items in the tree.
disabledKeysSet<Key>A set of keys for items that are disabled.
expandedKeysSet<Key>A set of keys for items that are expanded.
selectionManagerSelectionManagerA selection manager to read and update multiple selection state.

Methods

MethodDescription
toggleKey( (key: Key )): voidToggles the expanded state for an item by its key.

Example#


See the docs for useMenu in react-aria for an example of useTreeState.