Skip to content

Lesson 8 of 9 · 26m

Interop With React and Vue Without Glue-Layer Rot

How modern frameworks pass attributes and properties, why custom events differ from synthetic events, and when a thin wrapper is justified.

Web components are framework-agnostic, but integration is not magic. Frameworks make different decisions about lowercase props, object serialization, event binding, and SSR. The reliable pattern is thin: primitives as attributes, rich values as properties, DOM events as events.

React versions matter

React 19 substantially improved custom-element handling, but older React versions often serialized unknown props as attributes. The difference is fatal for object values:

<user-card user={{ name: "Ada" }} theme="compact" />

In a legacy or mixed environment, assign object properties through a ref:

const ref = useRef(null);
useEffect(() => {
  ref.current.user = user;
}, [user]);
return <user-card ref={ref} />;

This is not elegant, but it is honest. It beats corrupting data into user="[object Object]" and then blaming the component.

Custom events are DOM events

A stepper-change event is not automatically a reliable onStepperChange prop across React versions. The interop-safe pattern is explicit subscription:

useEffect(() => {
  const el = ref.current;
  const onChange = (event) => setValue(event.detail.value);
  el.addEventListener("stepper-change", onChange);
  return () => el.removeEventListener("stepper-change", onChange);
}, []);

Document the real DOM event name first. Framework conveniences are secondary bindings, not the source of truth.

Vue is closer, but configuration still matters

If Vue warns that <user-card> is unresolved, mark hyphenated tags as custom elements:

app.config.compilerOptions.isCustomElement = (tag) => tag.includes("-");

Then ordinary bindings can be natural:

<quantity-stepper min="0" :max="max" :value="count"
  @stepper-change="onStepperChange" />

The remaining trap is assuming kebab-case template syntax magically updates non-reflected object properties. It only works when the element actually exposes a coherent property contract.

Keep wrappers thin

A wrapper may translate framework idioms into DOM APIs: assign properties, subscribe to events, forward slots. It must not re-render internals, duplicate validation, or mirror every state. Once a wrapper knows implementation details, every component release becomes a framework release.

SSR adds one more constraint: complex properties cannot serialize into HTML. Provide primitive initial attributes or hydrate rich values after upgrade. Avoid moving server-rendered light DOM into shadow DOM during first upgrade; that causes layout shift and content loss when hydration fails.

Field notes

  • Test the oldest framework version you support; custom-element behavior changed materially over time.
  • In TypeScript, extend HTMLElementTagNameMap so element types and properties are visible to consumers.
  • Unstable inline callbacks can cause listener churn. Stabilize handlers or write cleanup correctly.
  • If framework listeners never see events, fix missing composed in the component before adding framework-specific hacks.

Was this lesson helpful?

Sign in to rate