Skip to content

Component Instance

When you mount a component, Ornata returns a component instance.

That instance becomes the integration point between the component and surrounding page code.

const instance = Counter.mount(root);

The public instance currently exposes:

  • root
  • state
  • dispose()
  • addStateListener()

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

state is the public reactive state object.

Updating writable public properties triggers the component update flow.

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

When a public state write succeeds, Ornata validates the new value, updates computed values, re-runs render callbacks, calls matching watch callbacks, and notifies state listeners.

dispose() unmounts the instance and runs cleanup behavior, including:

  • update cleanup
  • state listener cleanup
  • lifecycle.unmount()

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

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

The listener receives:

{
property,
newValue,
oldValue,
target,
}

The method returns a cleanup function that removes the listener.

Use this when code outside the component needs to observe public state changes.

For the conceptual difference between this and watch, see State Listeners and Watchers.

The component constructor also provides instance-management helpers:

  • mount(root, initialState?)
  • getInstance(root)
  • findInstance(root)
  • unmount(root)

Use getInstance() when absence should throw, and findInstance() when absence is an expected branch.

For a fuller walkthrough, read Instances and Mounting.