Skip to content

Component Instance

The API for a mounted component instance.

Component instances are created from the component constructor via the mount() or mountAll() method.

For more information, see Mounting Instances.

The public instance currently exposes:

root is the resolved root element the component was mounted on.

state is the public reactive state object.

Properties marked private or readonly in state options are protected from external reads and/or writes.

Updating writable public properties triggers the component update flow.

For broader guidance on reactive state behavior, see State and State Update Flow.

dispose() unmounts the instance and runs cleanup behavior.

Use addStateListener() to subscribe to a specific state property.

const cleanup = instance.addStateListener('count', (event) => {
console.log(event.newValue);
});

The listener receives an event object with the following properties:

PropertyDescription
propertyThe state property name that changed.
newValueThe incoming value for the update.
oldValueThe outgoing value for the update.
targetThe mounted instance that emitted the change.

The method returns a cleanup function that removes the listener.

To learn more, see State Listeners.