Lifecycle
Ornata supports two lifecycle hooks:
mountunmount
These hooks are useful when a component needs setup or cleanup behavior outside the normal render flow.
lifecycle.mount runs once when the component is mounted.
lifecycle: { mount() { console.log("Mounted"); },}lifecycle: { mount() { console.log("Mounted"); },}This is a good place for:
- starting timers
- registering observers
- connecting third-party integrations
- performing one-time activation logic
unmount
Section titled “unmount”lifecycle.unmount runs once when the component is disposed or unmounted.
lifecycle: { unmount() { console.log("Cleaned up"); },}lifecycle: { unmount() { console.log("Cleaned up"); },}This is a good place for:
- stopping timers
- disconnecting observers
- removing integration state
- general teardown logic
A practical example
Section titled “A practical example”const Clock = defineComponent({ 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); } }, },});const Clock = defineComponent({ name: "Clock", data: { intervalId: null as number | null, }, lifecycle: { mount() { this.data.intervalId = window.setInterval(() => { console.log("tick"); }, 1000); }, unmount() { if (this.data.intervalId !== null) { window.clearInterval(this.data.intervalId); } }, },});This is also a good example of why data exists: some values need to persist across lifecycle hooks without becoming reactive state.
See Data for that concept directly.
Lifecycle versus render
Section titled “Lifecycle versus render”Use render when the goal is to update DOM output.
Use lifecycle hooks when the goal is setup or cleanup around the component’s existence.
That distinction keeps the component easier to reason about.
Lifecycle versus watch
Section titled “Lifecycle versus watch”Use watch when behavior should happen because a specific state property changed.
Use mount and unmount when behavior should happen because the component started or stopped existing.