Open In App

ReactJS Web Components

Web components, encapsulated HTML tags for web development, are versatile, framework-agnostic elements native to HTML, usable seamlessly in various environments like React and Angular.

What are the types of web components?

There are three types of web components:



1. Custom Elements:

Steps to create a Custom Element:

Lifecycle methods for Custom Elements:

Example 1: In this example, we created a custom element with the tag <test-element>.




<!DOCTYPE html>
<html>
 
<head>
    <title>Web Components</title>
</head>
 
<body>
    <div>
        <h1>Custom Element</h1>
        <test-element></test-element>
    </div>
 
    <script type="text/javascript">
        class TestElement extends HTMLElement {
            constructor() {
                super();
 
                // Setting an attribute
                this.setAttribute('name', 'Marry');
            }
            connectedCallback() {
                this.innerHTML =
                    `<div>
                        <p>Hello ${this.getAttribute('name')}!</p>
                        <p>how are you</p>
                    </div>`
            }
        }
         
        // Register the custom element
        customElements.define('test-element', TestElement);
    </script>
</body>
 
</html>

Output:



Output

2. Shadow DOM: 

Example 2: In this example, we created a shadow DOM and attached it to our custom element.




<!DOCTYPE html>
<html>
 
<head>
    <title>Web Components</title>
</head>
 
<body>
    <div>
        <h1>Shadow DOM </h1>
        <test-element></test-element>
    </div>
 
    <script type="text/javascript">
        class TestElement extends HTMLElement {
            constructor() {
                super();
 
                // Setting an attribute
                this.setAttribute('name', 'ashton');
 
                // Attaches a shadow DOM tree to
                // <test-element> and returns a reference
                const shadowRoot =
                    this.attachShadow({ mode: 'open' });
            }
 
            connectedCallback() {
                this.shadowRoot.innerHTML =
                    `<style>
                            div {
                                background:cyan;
                            }
                        </style>
                        <div>
                            <p>Hello ${this.getAttribute("name")}!</p>
                            <p>Nice to meet you</p>
                        </div>`;
            }
        }
 
        // Register the custom element
        customElements.define('test-element', TestElement);
    </script>
</body>
 
</html>

Output:

Output

HTML Templates:

Example 3: In this example, we created a custom template to show the data of the students.




<!DOCTYPE html>
<html>
 
<head>
    <title>Web Components</title>
</head>
 
<body>
    <div>
        <h1>HTML Templates</h1>
        <template id="student_template">
            <style>
                li {
                    color: red;
                    list-style: none;
                }
            </style>
            <li>
                <span class="name"></span> —
                <span class="score"></span>
            </li>
        </template>
        <test-element></test-element>
        <ul id="students"></ul>
    </div>
 
    <script type="text/javascript">
        class TestElement extends HTMLElement {
            constructor() {
                super()
                const shadowRoot =
                    this.attachShadow({ mode: "open" });
 
            }
            connectedCallback() {
                let students = [
                    { name: "Emma", score: 94 },
                    { name: "Ashton", score: 72 },
                    { name: "Hannah", score: 86 },
                    { name: "Steve", score: 90 },
                    { name: "Amy", score: 75 },
                ]
 
                students.forEach(student => {
                    let template =
                        document.getElementById("student_template");
                    let templateContent =
                        template.content.cloneNode(true);
 
                    // Append a clone of the template
                    // content to the shadow root
                    this.shadowRoot.appendChild(templateContent);
 
                    this.shadowRoot.querySelector('.name')
                        .innerHTML = student.name // Add the name
                    this.shadowRoot.querySelector('.score')
                        .innerHTML = student.score // Add the score
 
                    // Append the list to the ul students
                    document.getElementById("students")
                        .appendChild(this.shadowRoot)
                })
            }
        }
         
        // Register the custom element
        customElements.define('test-element', TestElement);
    </script>
</body>
 
</html>

Output:

Output

Third-Party Web Available Components:


Article Tags :