Skip to content

State Listeners

addStateListener() is the public subscription API on a mounted Ornata component instance.

It lets code outside the component observe changes to a specific public state property.

const instance = Counter.mount("[data-counter]");
const cleanup = instance.addStateListener("count", (event) => {
console.log(event.newValue);
});

The listener receives an event object with:

  • property
  • newValue
  • oldValue
  • target
instance.addStateListener("count", ({ property, newValue, oldValue, target }) => {
console.log(property, newValue, oldValue, target);
});

addStateListener() returns a cleanup function.

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

That makes it easy to use listeners in page-level orchestration code or temporary integrations.

Use addStateListener() when:

  • external code needs to observe a component
  • another system needs to react to state changes
  • you are coordinating behavior between mounted instances
  • you want subscriptions without putting more logic inside the component itself

These APIs are related, but they are for different layers of your application:

  • watch is for internal component-side reactions
  • addStateListener() is for external instance-side subscriptions

That distinction keeps component behavior and integration code from blurring together.

State listeners observe public state changes.

That means the component’s state contract still matters:

  • listeners are attached to known state properties
  • private and readonly rules still shape how external code can interact with state

State listeners are for external observers.

They are a clean way to integrate a mounted Ornata component into wider page behavior without reaching into its internals.