webtides / element-js

Simple and lightweight base classes for web components with a beautiful API
MIT License
28 stars 3 forks source link

Children not set inside connected callback #15

Closed lukas-schardt closed 4 years ago

lukas-schardt commented 4 years ago

If you are trying to access the children of the current element inside the connected-Hook, the HTMLCollection will be empty.

    connected() {
        this._children = [ ...this.children ];
        console.log(this._children, this.children );
    }

But if we are requesting an animation frame it works and the array is not empty anymore

    connected() {
        requestAnimationFrame(() => {
            this._children = [ ...this.children ];
            console.log(this._children, this.children );
        });
    }
quarkus commented 4 years ago

This occured due to early script execution time. It seems like the browser does not know the children of a custom element in the connectedCallback when the script is executed before the elements dom position.