Disclaimer: This blog post suggests using setTimeout
This post was originally written as StackOverflow answer in February 2022:
WTF? this.i...
For further actions, you may consider blocking this person and/or reporting abuse
So, the best thing to do is just putting all the JavaScript code on the bottom of the body tag, as always, let the browser create a myriad of
UnknownHTMLElementinstances, the let it upgrade all instances whencustomElement.define()triggers. That opens the door to inability to interact and FOUC (en.wikipedia.org/wiki/Flash_of_uns...) for me, and yet it seems to be the best approach.When I first started to learn about web components, I already knew Angular so I thought "ok
connectedCallback()is likengOnInit()" and actually it is, because if you need to read the parsed children you need to callngAfterViewInit()andngAfterContentInit()which are Angular-specific methods to interact with inner HTML, notngOnInit(). Still,connectedCallback()seemed to be used by other developers more like "run this when the DOM is ready to be queried" more than "run this when the component is attached to the DOM, but maybe it's not ready yet". I guess it's just a little confusing all around and frameworks do a much better job than "standards" in giving simple flows and guarantees for me.React is different, because a
useEffect()without dependencies actually triggers after any first render (I'd say almost like asetTimeout()) and it actually works as intended.Wouldn't a much simpler
"readystatechange"event listener get you like 98% of the way there?Like, I imagine something like (untested):
Edit: As I am working on a custom element right now, I decided to try this, and noticed two things:
EDIT: Added a
console.logto both examples to make it clearer where the actual initialisation code goes.Depends how you do it, I guess?
I assume you're mostly considering using
innerHTMLhere, which I tend to stay away from whenever possible, so to me that falls in the remaining 2%.Building a custom element in JS (including children) and then inserting it into the page will run the callback on a populated component. If you want to add elements afterwards, then you probably need a MutationObserver anyway, because you'll likely want to continue monitoring for changes for the entirety of the components life-cycle, not just for some initial "setup" phase where you continue adding children manually.
It has nothing to do with
innerHTMLIt doesn't matter how you add DOM to your existing page,readystatechangewill never fire again after the first pageload.I don't think I understand what requirements you have towards your custom elements then.
I see two settings:
a) A custom element should consider its child elements when it is created and do some stuff with those, but can afterwards become inert because the contents of the element are static at that point.
b) The component should respond to insertions and deletions throughout its entire lifetime and update its internal state accordingly on every change.
In the first case, the site load is the only problematic part, because the element gets inserted into the DOM before its children have been parsed. When creating the element from javascript, the child elements are already there by the time the element is connected, so the
connectedCallbackwill work just fine.In the latter case, inserting items initially is really just a not-so-special case of inserting items at any point during the object's lifecycle, so no special code is required here.
The dynamic case can usually be achieved relatively easily with a generic
MutationObserverthat dispatches an event or calls a method on its target.There is no requirement, there is a fact.
This blog is (an attempt) to explain why there is no innerHTML when the Web Component is defined before the DOM is parsed
Almost all Web Components posted on X in the last month, fall into this trap.. and don't work in 100% of cases.
Yes, you can solve part of the issue with a MutationObserver; that is like taking a sledgehammer to drive in a nail.
I've spent a lot of time over the past few days researching the problem of how to access the children of a shadow root in a custom element, including the discussions linked in the post and the HTML standard. This blog post is the only thing that pointed me toward a solution, so thank you for that. I tried the readystatechange approach but it didn't work in my application. Even when readystate was complete, the children still were not available. I was reluctant to use setTimeout(), because there's no way to know how long the timeout needs to be. I chose setInterval() instead, like so
Can you create a JSFiddle where setInterval fires more than once? The only difference with setTimeout is that setInterval runs again.. most likely it uses the same timeout under the hood.
Ok, so I decided to show more of my code. In the childrenReady method, I added a counter and set the interval to 1 millisecond.
After navigating back and forth a few times, this is the result from the console.
The main page of this app includes seven components, three of which use the childrenReady method. The console says that 1 component fired 6 times and the other two each fired 4 times before the children were ready.
How is
childrenReadytriggered? Immediatly from theconnectedCallback?I have a JSFiddle test with all methods and would like to add your code to it.
this.shadowRoot.children.length > 0is not the same asthis.children.length > 0Which (I presume) can trigger when a large DOM is still being parsed, so it doesn't signal all childrenReady, but some childrenReady?
That's a big ask for me right now. I'll see what I can do. I can tell you that I had to setTimeout to 10 milliseconds in my code before it would work. There's no guarantee that every instance is going to take the same amount of time, especially if it's running on different machines and different browsers. If the children aren't ready when the timeout ends, your code fails. You can't really take that risk. setInterval solves that problem. I hadn't thought of putting in a counter to see how many times it fires.