Skip to content

Persistent Data

This example shows why data is useful for values that need to persist inside a reusable component without participating in rendering.

Open this demo in a new tab

import { defineComponent } from "ornata";
export const Clock = defineComponent<{
data: {
intervalId: number | null;
};
}>({
name: "Clock",
data: {
intervalId: null,
},
lifecycle: {
mount() {
this.data.intervalId = window.setInterval(() => {
console.log("tick");
}, 1000);
},
unmount() {
if (this.data.intervalId !== null) {
window.clearInterval(this.data.intervalId);
}
},
},
});

intervalId needs to:

  • persist for the life of the mounted instance
  • be available in both mount and unmount
  • avoid triggering the render/update flow

That makes data the right fit.

If the value should drive DOM updates or public component behavior, use state instead.