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
version3.34.0
usageimport {useTreeState} from 'react-stately'

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.
setExpandedKeys( (keys: Set<Key> )): voidReplaces the set of expanded keys.

Example#


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