webcomponents / webcomponents/custom-elements-everywhere

React and Vue strip attributes

Open
#2,551 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.3k
Forks
108
PR merge metrics
No merged PRs in 30d

Description

I'm trying to understand how React and Vue can report 100% compatibility. Here's an example of a simple Hello World web component that works fine in vanilla web apps and in web apps that use Solid or Svelte, but will not work with React or Vue.

```js
class HelloWorld extends HTMLElement {
name = "";

constructor() {
super();
this.attachShadow({ mode: "open" });
}

connectedCallback() {
this.name = this.getAttribute("name") || "World";
this.render();
}

render() {
const { shadowRoot } = this;
if (shadowRoot) shadowRoot.innerHTML = `

Hello, ${this.name}!

`;
}
}

if (!customElements.get("hello-world")) {
customElements.define("hello-world", HelloWorld);
}
```

An example of using this custom element is ``.

The problem is that when React and Vue see that the web component has a property with the same name as an attribute, they set the property to the attribute value and delete the attribute. When `connectedCallback` is called and that attempts to get the attribute value, `null` is returned. The web component could be modified to handle the case of the property already being set, but that feels wrong because it's a change that is only needed for compatibility with React and Vue.

Should React and Vue practice of removing attributes be considered wrong?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.