最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - custom element setup: constructor vs connectedCallback - Stack Overflow

programmeradmin3浏览0评论

I'm new to web ponents, and I've noticed some examples set the dom in the custom-element's constructor, while others do it in the connectedCallback. As both seem to work fine(although I tried only Chrome), I assume the main difference is the case in which a user creates the element in js and not attaching it to the page?

I guess the main question here is whether I'm missing some other reason to prefer one method over the other.

Thanks.

I'm new to web ponents, and I've noticed some examples set the dom in the custom-element's constructor, while others do it in the connectedCallback. As both seem to work fine(although I tried only Chrome), I assume the main difference is the case in which a user creates the element in js and not attaching it to the page?

I guess the main question here is whether I'm missing some other reason to prefer one method over the other.

Thanks.

Share Improve this question edited Jan 29, 2020 at 15:47 connexo 56.8k15 gold badges108 silver badges145 bronze badges asked Jan 29, 2020 at 15:10 Dor CohenDor Cohen 1953 silver badges7 bronze badges 1
  • Could you give examples of both scenarios? – Emiel Zuurbier Commented Jan 29, 2020 at 15:14
Add a ment  | 

1 Answer 1

Reset to default 20

Best practices and rules for custom element constructors

What's safe to do in the constructor

In the constructor, it's safe to

  • create the shadow root;
  • create and append elements *;
  • attach event listeners to those elements (scoped to your own ShadowDOM);
  • try to read attributes, just be aware that these can only be present in the upgrade case (declarative usage)

What you cannot do in the constructor

In the constructor, you are not allowed (amongst other things)

  • to create attributes...
  • to access child elements (do not confuse these with elements in your shadowRoot!)...

...because those might not be present in the non-upgrade case, and definitely won't be present when you dynamically create your custom element using either document.createElement('my-custom-element') or new MyCustomElement().

What's unwise to do in the constructor

In the constructor, you probably don't want to

  • attach event listeners to elements outside of the ponent's shadow DOM (like e.g. document, window), because these are the kind of listeners you should clean up in your ponent's disconnectedCallback (which will be also be called when e.g. your ponent is moved in the DOM, so after removing them in the disconnectedCallback they need to be re-added when re-connecting).

Attaching these listeners in the constructor and properly cleaning them up in the disconnectedCallback results in missing listeners once your ponent gets removed from (and later re-added), or moved in, the DOM.

*Pitfalls and things to be aware of

You need to be aware of the custom element lifecycle to not fall into otherwise obvious pitfalls, which include:

  • If you add attributes in the constructor and have included those in your ponent's observedAttributes, remember this will immediately trigger the attributeChangedCallback for those attributes, even if your element is not yet connected (a.k.a. in the DOM).
  • If you create and append other custom elements into your ponent's shadow DOM, remember this will trigger those ponents' connectedCallback.

In part, these best practices and rules follow https://html.spec.whatwg/multipage/custom-elements.html#custom-element-conformance, in other parts they deviate from remendations done there in the spec.

Specifically I disagree on the following (given the scope for the listeners is outside the ponent), for the reasons I gave above.

In general, the constructor should be used to set up initial state and default values, and to set up event listeners and possibly a shadow root.

发布评论

评论列表(0)

  1. 暂无评论