Managing State
With Headless Tree, there are multiple ways how to manage the state of the tree. By default, Headless Tree manages all parts of the state itself, and you just need to provide the data. Alternatively, you can opt into managing the entirety of the tree state yourself, which is useful if you want to change the state from outside, or read from the state from outside. Finally, you can also manage individual parts of the state.
Note that the tree state only includes information that relates to how the tree is displayed, not parts
that are specific to the data that composes the tree. For example, when you include the drag-and-drop feature,
a dnd
state sub-state is exposed that contains the IDs of dragged items, the item that the user is currently dragging
over, and the position of where the item would be dropped. When you choose to manage that state yourself, you can
read those pieces of information or control them yourself. However, the data of the tree, as well as its mutation
followed by the drop event, is not part of the state. You need to handle these data mutations yourself outside of the
tree state.
Another important part to keep in mind is that which parts of the state are exposed depends on the features that
are included. The aforementioned dnd
state property will be completely ignored if the dragAndDropFeature
is
not included in the tree configuration, as will be dnd-related update handlers.
Letting headless-tree manage the state
The simplest option is to let Headless Tree manage the entirety of the state. This is done by default, so you
do not have to do anything. You can also supply a initialState
prop in the tree configuration to define
the initial state.