Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between constructor and connectedCallback in custom elements v1

Tags:

I am new to web development and recently I have been seeing much debate and talks about Custom Elements v1. They allow you to define your own elements with their own custom behaviors and if Shadow DOM is used, with scoped styles.

When I was learning about it in this site, I did not understand the table under "Custom element reactions". Can anyone please explain the difference between using the "constructor" and "connectedCallback" and also the terms "created or upgraded" and "inserted into the DOM"?.

For extra information, my custom element's definition is in a separate file and it uses shadow DOM. I used HTML Import to import the element's definition into the main document.

like image 723
Shashank Avatar asked Nov 08 '16 16:11

Shashank


People also ask

What is the difference between constructor and connectedCallback?

An element is upgraded when it already exists before definition - at the point of definition the constructor is then called automatically. If the element was already attached to the DOM at this point connectedCallback will also be called.

What is connectedCallback?

connectedCallback : Invoked each time the custom element is appended into a document-connected element. This will happen each time the node is moved, and may happen before the element's contents have been fully parsed. Note: connectedCallback may be called once your element is no longer connected, use Node.

What's the difference between Web Components and custom elements?

Web Components consist of three separate technologies that are used together: Custom Elements. Quite simply, these are fully-valid HTML elements with custom templates, behaviors and tag names (e.g. <one-dialog> ) made with a set of JavaScript APIs. Custom Elements are defined in the HTML Living Standard specification.

Can custom elements be self closing?

Yes. Custom elements require a closing tag. Only certain tags in HTML are allowed to be self-closing due to the parser.


1 Answers

As already stated by Juvian in the comments:

  • constructor() is called when the element is created.
  • connectedCallback() is called when (after) the element is attached to the DOM.

The constructor() call is not specific to Custom Elements, it occurs with any object creation (typically created with the new operator), and not only HTML Elements.

In the constructor() call, you can create the Shadow DOM, but you can't add Nodes inside the normal DOM, and you can't add or set an attribute either.

About upgrade:

The upgrade occurs when an unknown tag declared in the HTML code is defined afterwards (by the customElements.define() method). The "unknown" element becomes a "custom" element. The constructor() and connectedCallback() methods are then called.

Note: when an element is created (as unknown), and then defined, it is upgraded only when attached.

class CE extends HTMLElement {    constructor() {      super()      console.info( 'constructed' )    }    connectedCallback() {      console.info( 'connected' )      this.innerHTML = 'Hello'  //can't be set in constructor()    }  }      B1.onclick = function () {    ce = document.createElement( 'c-e' )    ce.textContent = 'unknown'  }    B2.onclick = function () {    document.body.appendChild( ce )  }    B3.onclick = function () {    customElements.define( 'c-e', CE)    }
<button id=B1>create</button>  <button id=B2>attach</button>  <button id=B3>define</button>

Try different combinations with the above snippet:

  • Run then: 1 Create - 2 Attach - 3 Define
  • Run then: 1 Create - 2 Define - 3 Attach
  • Run then: 1 Define - 2 Create - 3 Attach
like image 83
Supersharp Avatar answered Dec 15 '22 20:12

Supersharp