Dev.Lib
arrow_backBack to Blogs
LEVEL_077.5June 27, 20261 min read

Custom Elements lifecycle

Web Platform APIs

`connectedCallback`, `disconnectedCallback`, `attributeChangedCallback`, `adoptedCallback` — mirror mount/unmount/props.

Summary

connectedCallback, disconnectedCallback, attributeChangedCallback, adoptedCallback — mirror mount/unmount/props. Upgrade undefined → defined when script loads. Sync vs async definition affects parser. Framework wrappers must forward lifecycle correctly.

Code example

class UserCard extends HTMLElement {
  static get observedAttributes() { return ['user-id']; }

  connectedCallback() { this.render(); }
  disconnectedCallback() { this.cleanup?.(); }
  attributeChangedCallback() { this.render(); }
}

Key points

  • connectedCallback, disconnectedCallback, attributeChangedCallback, adoptedCallback — mirror mount/unmount/props.
  • Upgrade undefined → defined when script loads.
  • Sync vs async definition affects parser.
  • Framework wrappers must forward lifecycle correctly.

Further reading

Related concepts