Web Components Inside React, Vue & Angular

Web Components Inside React, Vue & Angular in JavaScript: The selling point of Web Components is that they outlive framework churn.

Concept Explanation

The selling point of Web Components is that they outlive framework churn. A <date-picker> you ship today still works when your team migrates from React 18 to whatever comes next.

That promise is real, but the seams are not free. Every framework talks to the DOM slightly differently, and custom elements have two channels of input: attributes (strings only) and properties (any JS value). Frameworks disagree about which one they use, and that disagreement is where every bug lives.

The mental model: attributes are the mail slot, properties are the front door. Strings fit through the slot; objects and functions need the door.


Syntax or Core Structure

Vue and Angular: mostly free

<!-- Vue: .prop forces a property, @ listens to a CustomEvent -->
<date-picker :value.prop="selectedDate" @change="onChange" />

<!-- Angular: [ ] sets a property, ( ) listens to an event -->
<date-picker [value]="selectedDate" (change)="onChange($event)"></date-picker>

React 18 and earlier: needs a ref

React sets unknown props as attributes and does not listen to custom events, so you wire both by hand.

function DateField({ value, onPick }) {
  const ref = useRef(null);

  useEffect(() => {
    const el = ref.current;
    el.value = value;                       // property, not attribute
    const handler = (e) => onPick(e.detail);
    el.addEventListener('change', handler);
    return () => el.removeEventListener('change', handler);
  }, [value, onPick]);

  return <date-picker ref={ref} />;
}

React 19 fixed most of this: it passes objects as properties and supports onChange-style custom event props directly.

Talking back out

this.dispatchEvent(new CustomEvent('change', {
  detail: { date: '2026-01-01' },
  bubbles: true,
  composed: true   // required to escape the shadow boundary
}));

Real World Example

A design system for a company with three stacks. Shoelace and Adobe Spectrum both ship exactly this way: one set of custom elements, plus thin per-framework wrappers generated automatically. The component logic is written once; the wrappers only translate props and events. When a fourth stack appears, you write a fourth wrapper, not a fourth button.


Common Mistakes

  • Omitting composed: true. The event fires, bubbles up to the shadow root, and stops there. The parent framework never hears it and you assume the component is broken.
  • Passing an object as an attribute. <my-chart data="[object Object]"> is what you get. Objects must go through properties.
  • Server-side rendering without a guard. customElements.define throws in Node. Register inside a browser-only effect or lazily import the definition.
  • Registering the same tag twice. Hot reload plus a module loaded from two paths gives you NotSupportedError. Guard with if (!customElements.get('my-tag')).

Best Practices

  • Accept strings via attributes for simple config, and reserve properties for rich data. Reflect important state back to attributes so CSS can react to it.
  • Ship a thin framework wrapper alongside the component rather than making every consumer write refs.
  • Name events without a framework prefix (change, not onChange) and put payloads on detail.
  • Test the component in a plain HTML page first. If it needs a framework to work, it is not really a Web Component.

Practice Exercises

  1. The Bridge: Wrap any custom element in a React component that forwards one object property and one custom event, with proper cleanup in the effect return.
  2. The Escape Artist: Dispatch a CustomEvent from inside a shadow root without composed: true, confirm the outer listener never fires, then add the flag and watch it work.
  3. The Double Registration: Import the same component definition twice and reproduce the NotSupportedError, then fix it with a customElements.get guard.