Lesson 1 of 9 · 24m
Custom Elements: The Lifecycle That Actually Runs
Autonomous custom elements, registration timing, constructor rules, and the lifecycle traps that only appear after hydration or navigation.
The first custom element most teams ship works in the demo and breaks in the product. Elements are upgraded, moved, cloned, disconnected, and reconnected by the browser at times framework intuition doesn't predict. Treat the element as a DOM object with a lifecycle, not as a tiny application.
Registration is the event horizon
class UserBadge extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
}
connectedCallback() {
this.render();
}
}
customElements.define("user-badge", UserBadge);Until define runs, <user-badge> is an inert HTMLElement. When registration happens, parser-created elements already in the document upgrade in place: constructor first, then connectedCallback if connected. Code running before and after registration sees different objects. That ordering bites test setup, hydration, and scripts that assume a tag is already upgraded.
Constructor discipline
The constructor is not the place to read attributes, measure layout, query children, or touch light DOM. During parsing, children often do not exist yet. Put only invariant setup there:
constructor() {
super();
this.attachShadow({ mode: "open" });
this._onClick = this._onClick.bind(this);
}Render in connectedCallback, idempotently or behind a first-render guard. If the element is moved and reconnected, rebuilding everything can erase focus and transient internal state.
connected and disconnected are symmetrical
connectedCallback can run repeatedly: routers remove pages, caches detach trees, and parents move nodes. Pair every subscription, observer, timer, and listener with teardown:
connectedCallback() {
this._ro = new ResizeObserver(() => this._layout());
this._ro.observe(this);
}
disconnectedCallback() {
this._ro?.disconnect();
}Duplicated listeners after reconnection are the custom-element version of a leaked effect. In long-lived applications they show up as mysteriously increasing work after navigation.
attributeChangedCallback is a narrow door
Only attributes listed in observedAttributes trigger the callback:
static observedAttributes = ["name", "tone"];
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue !== newValue) this.render();
}Boolean HTML semantics still apply: presence means true. <user-badge disabled> and disabled="false" both mean disabled. Use enum-like strings such as tone="neutral" or properties for richer values.
Field notes
- Use
customElements.whenDefined("user-badge")when dependent code must wait for upgrade. - Never define the same tag twice; registration is global. Hot-reload tooling needs an explicit strategy.
- Server-rendered HTML can emit the final tag before JavaScript runs, letting the element enhance rather than create everything.
- If initial render uses
innerHTML, never interpolate untrusted user data into the template.
Was this lesson helpful?