Web Components and React Integration

Web Components are a set of browser standards that allow developers to create reusable, encapsulated HTML elements that can be used across frameworks. They work natively in the browser without requiring any external libraries. In modern applications, integrating Web Components with React can bring several advantages, like encapsulating complex UI components or using existing Web Components in a React app.This chapter will cover everything from basic concepts to advanced topics related to integrating Web Components with React, with plenty of examples to ensure a deep understanding.

What are Web Components?

Web Components is a set of technologies that allow you to create reusable, encapsulated HTML elements. The core technologies of Web Components are:

  • Custom Elements: Define new HTML tags.
  • Shadow DOM: Encapsulate styles and DOM within the component.
  • HTML Templates: Reusable HTML snippets.

These components can be used across different frameworks like React, Angular, or Vue, or even in vanilla JavaScript.

Example:

A basic Web Component for a custom button might look like this:

				
					class MyButton extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    const button = document.createElement('button');
    button.textContent = 'Click Me!';
    this.shadowRoot.appendChild(button);
  }
}

customElements.define('my-button', MyButton);

				
			

This component defines a new HTML element <my-button> that encapsulates its content and behavior.

The Benefits of Using Web Components in React

There are several benefits to integrating Web Components with React:

  • Reusability: You can build components that work across different projects and frameworks.
  • Encapsulation: Web Components allow styles and DOM to be isolated from the rest of the application, preventing conflicts.
  • Interoperability: They allow you to use non-React components (such as a Web Component library) within React applications.

Web Components are particularly useful when:

  • You need a standalone component that can be shared across different apps.
  • You’re integrating existing Web Components into a React-based application.

Web Components Lifecycle and React’s Rendering Process

Web Components have their lifecycle methods, much like React components:

  • ConnectedCallback: Called when the element is added to the document.
  • DisconnectedCallback: Called when the element is removed from the document.
  • AttributeChangedCallback: Called when an attribute of the component is changed.

However, React uses its own virtual DOM and rendering process, so integrating these lifecycles can sometimes be tricky. React components have their own lifecycle methods such as componentDidMount and componentWillUnmount.

When integrating Web Components with React, understanding how these two lifecycles interact is essential

Creating a Simple Web Component

Let’s create a simple Web Component that displays a counter. This component will have a button to increase the counter.

Example:

				
					class CounterComponent extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.counter = 0;

    const wrapper = document.createElement('div');
    const button = document.createElement('button');
    button.textContent = 'Increase Counter';
    
    const counterDisplay = document.createElement('p');
    counterDisplay.textContent = `Counter: ${this.counter}`;
    
    button.addEventListener('click', () => {
      this.counter++;
      counterDisplay.textContent = `Counter: ${this.counter}`;
    });
    
    wrapper.appendChild(button);
    wrapper.appendChild(counterDisplay);
    this.shadowRoot.appendChild(wrapper);
  }
}

customElements.define('counter-component', CounterComponent);

				
			

In this example, the component encapsulates a counter functionality, updating the value whenever the button is clicked.

Using Web Components in React

React can render Web Components just like any other HTML element. However, there are certain steps needed for integration, such as ensuring that attributes and events are passed properly between React and the Web Component.

Example: Using the counter-component in React

				
					import React from 'react';

class App extends React.Component {
  render() {
    return (
      <div>
        <h1>React and Web Component Example</h1>
        <counter-component></counter-component>
      </div>
    );
  }
}

export default App;

				
			

In this example, the <counter-component> is used directly in the JSX. React treats it as a regular DOM element.

Passing Props and Handling Events Between React and Web Components

To pass data (props) and handle events between React and Web Components, you need to use ref to interact with the Web Component’s properties and methods.

Passing Props:

You can use the standard attributes of HTML to pass data to Web Components, but complex data structures need to be set programmatically.

Example

				
					import React, { useRef, useEffect } from 'react';

const App = () => {
  const webComponentRef = useRef(null);

  useEffect(() => {
    if (webComponentRef.current) {
      webComponentRef.current.setAttribute('counter', 5);
    }
  }, []);

  return <counter-component ref={webComponentRef}></counter-component>;
};

export default App;

				
			

Handling Events:

Web Components use custom events, which need to be listened for in React.

Example:

				
					useEffect(() => {
  const handleCounterIncrease = (event) => {
    console.log('Counter increased:', event.detail);
  };

  const webComponent = webComponentRef.current;
  webComponent.addEventListener('counter-increase', handleCounterIncrease);

  return () => {
    webComponent.removeEventListener('counter-increase', handleCounterIncrease);
  };
}, []);

				
			

Using Third-Party Web Components in React Applications

Many third-party libraries provide reusable Web Components. You can easily integrate these into React applications using the same techniques discussed earlier. Make sure to check the documentation of the specific Web Component library to see how to pass attributes and listen to events.

Styling Web Components in React

Web Components encapsulate styles using the Shadow DOM. However, styling Web Components from the outside (such as from a React application) can be a challenge since the styles are scoped within the component.

Example of Internal Styles:

				
					class StyledComponent extends HTMLElement {
  constructor() {
    super();
    const shadow = this.attachShadow({ mode: 'open' });
    const style = document.createElement('style');
    style.textContent = `
      p {
        color: blue;
      }
    `;
    shadow.appendChild(style);
  }
}

				
			

To style Web Components globally, you can avoid using the Shadow DOM or expose some properties as CSS custom properties (variables).

Polyfills and Browser Compatibility

While Web Components are supported by most modern browsers, older browsers might not fully support the Web Components specification. You may need to use polyfills (such as the @webcomponents/webcomponentsjs polyfill) to ensure compatibility.

Advanced Topics: Custom Elements and Shadow DOM

  • Custom Elements: Allow developers to create their own HTML tags with custom behavior.
  • Shadow DOM: Provides style and DOM encapsulation, ensuring that a Web Component’s internal structure doesn’t interfere with the rest of the application.

Example of a Custom Element with Shadow DOM:

				
					class CustomShadowElement extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.innerHTML = `<style>p {
          color: red;
        }</style><p>This is inside the Shadow DOM!</p>
    `;
  }
}
customElements.define('custom-shadow-element', CustomShadowElement);

				
			

In this example, the paragraph inside the Shadow DOM is encapsulated and won’t be affected by external styles.

Integrating Web Components with React provides the flexibility to reuse custom-built components across different projects and frameworks. By understanding how to manage the interaction between React and Web Components—such as passing data, handling events, and dealing with styles—developers can unlock the full potential of this integration. Web Components also help bridge the gap between modern web standards and React’s component-based architecture. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India