React Aria and React Spectrum have a new home!
useTreeState
Provides state management for tree-like components. Handles building a collection of items from props, item expanded state, and manages multiple selection state.
| install | yarn add react-stately |
|---|---|
| version | 3.42.0 |
| usage | import {useTreeState} from 'react-stately' |
API#
useTreeState<T extends object>(
(props: TreeProps<T>
)): TreeState<T>
Interface#
Properties
| Name | Type | Description |
collection | Collection<Node<T>> | A collection of items in the tree. |
disabledKeys | Set<Key> | A set of keys for items that are disabled. |
expandedKeys | Set<Key> | A set of keys for items that are expanded. |
selectionManager | SelectionManager | A selection manager to read and update multiple selection state. |
Methods
| Method | Description |
toggleKey(
(key: Key
)): void | Toggles the expanded state for an item by its key. |
setExpandedKeys(
(keys: Set<Key>
)): void | Replaces the set of expanded keys. |
Example#
See the docs for useMenu in react-aria for an example of useTreeState.