Lifecycle
Every Jadis component follows the Custom Elements lifecycle. Jadis exposes the most important hooks as protected methods you can override.
Lifecycle Hooks
onConnect()
Called on the task after the component is connected, once its template has been rendered. This is the place to set up event listeners, fetch data, or perform connection-specific setup. It runs again each time the component reconnects (moving it in the DOM counts), on the same DOM: make it safe to repeat, and guard one-time work (filling a list, a first fetch) by what the DOM already holds.
class MyComponent extends Jadis {
onConnect(): void {
// Component is now in the DOM — safe to interact with it
console.log('Component connected');
}
}onDisconnect()
Called when the component is removed from the DOM. Use this for cleanup that is not tied to killSignal, such as canceling timers or stopping external work. Listeners registered through Jadis helpers are cleaned up automatically.
class MyComponent extends Jadis {
private _intervalId?: number;
onConnect(): void {
this._intervalId = window.setInterval(() => {
console.log('ticking...');
}, 1000);
}
onDisconnect(): void {
clearInterval(this._intervalId);
console.log('Component disconnected');
}
}Connection State
You can check whether a component is currently connected to the DOM using the isConnected getter. It matters after an await: the component may have been removed meanwhile. Pass killSignal to fetch, so a removed component stops waiting:
class ItemList extends Jadis {
static readonly selector = 'item-list';
templateHtml(): Node {
return <ul></ul>;
}
async onConnect(): Promise<void> {
const response = await fetch('/api/items', { signal: this.killSignal });
const items: string[] = await response.json();
if (!this.isConnected) {
return;
}
this.getElement('ul').replaceChildren(...items.map((item) => <li>{item}</li>));
}
}Lifecycle Timeline
Constructor → connectedCallback (render template) → onConnect → ... → disconnectedCallback → onDisconnect- Constructor — Runs when the class is instantiated. Shadow DOM is attached here if
useShadowDomistrue. connectedCallback()— Runs when the component is connected. On the first connection it renderstemplateHtml()andtemplateCss(); the rendered DOM is reused on reconnection. It then applies theuseChangevalues set before the connection, then the attribute callbacks, all synchronously, before schedulingonConnect().onConnect()— Runs asynchronously after the component is connected. It runs again after each reconnection.- Active — The component is in the DOM and responding to user interaction.
disconnectedCallback()/onDisconnect()— The callback abortskillSignaland then callsonDisconnect()when the component is removed.
Important Notes
- Templates render only on the first connection; reconnection reuses the existing DOM.
onConnect()runs on a later task, not synchronously insideappendChild(). The template and the values set before the connection are already in place whenappendChild()returns.- The
killSignalis automatically aborted on disconnect, canceling listeners registered viathis.on(),useEvents(), andonBus(). - Avoid heavy work in the constructor. Defer connection-specific work to
onConnect().
See Also
- First Component — A practical example using lifecycle hooks.
- Event Handling — Using
this.on()for auto-cleaned event listeners. - killSignal — The built-in cleanup signal.