If I have 2 Lit elements, with a parent-child relationship, e.g.:
<x-parent>
<x-child></x-child>
</x-parent>
Are the connectedCallback
calls executed in a deterministic order?
In my tests looks like the connectedCallback
for the parent is always called first, but I don't know if I can rely on this being always the case.
UPDATE AFTER @justin-fagnani answer
@justin-fagnani Not sure if we are talking about the same, you wrote about when the component is defined, but my question is about the order of the connectedCallback. Anyways, based on your answer, I think that the child callback happens first if it is rendered using a <slot>
:
render() {
return html`<slot></slot>`;
}
But parent callback happens first if the child is rendered with a lit-html template:
render() {
return html`<x-child></x-child>`;
}
Is my assumption correct?
connectedCallback: Is 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.
This is equivalent to ngOnInit (Angular) and componentDidMount (react) lifecycles,
So yes parent method will be invoked first.
Ref: Link to mdn docs