This is a conceptual question for those who understand custom elements without seeing concrete code examples.
I'm creating a custom element that's display depends on its textContent. In the custom element's constructor, if I try to access this.textContent
it returns an empty string, even though the html of that custom element does indeed contain text.
In order to obtain the textContent
during the custom element's construction, I enclosed my constructor code within a setTimeout and then was able to make my custom elements construction be base on its textContent
.
However, this felt a little hacky and I figured there was a more proper way to obtain textContent during an element's construction. That's when I found Using the life Cycle Callbacks.
The connectedCallback
method allowed me to see textContent without putting a setTimeout into the custom element's constructor. However, this quote concerns me:
connectedCallback: . . . may happen before the element's contents have been fully parsed.
This worries me that if I have a lot of content inside the custom element, this.textContent
may still return an empty string because all that text may not yet be "fully parsed".
Is this worry justified? Is there a more sure way to obtain this.textContent
as a basis for your custom element's construction? Or, should I go with my initial solution of putting a setTimeout within the custom element's constructor?