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.0.0-alpha.1
usageimport {useTreeState} from '@react-stately/tree'

API#


useTreeState<T>( (props: CollectionBase<T> & Expandable & MultipleSelection )): TreeState<T>

Interface#


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.
toggleKey( (key: Key )) => voidToggles the expanded state for an item by its key.
selectionManagerSelectionManagerA selection manager to read and update multiple selection state.

Example#


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