-
Notifications
You must be signed in to change notification settings - Fork 47.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using a WebComponent/customElement with React #8249
Comments
I followed the examples pointed to from this page: https://facebook.github.io/react/docs/web-components.html Does anyone know if this page is up to date or accurate? |
This is not an issue with React. It appears that the problem is with how Babel transpiles Have you tried not using ES6 classes for this, just like the example you linked to does? For example, notice no // Define WebComponent
var proto = Object.create(HTMLElement.prototype, {
attachedCallback: {
value: function() {
var mountPoint = document.createElement('span');
this.createShadowRoot().appendChild(mountPoint);
var name = this.getAttribute('name');
var url = 'https://www.google.com/search?q=' + encodeURIComponent(name);
ReactDOM.render(<a href={url}>{name}</a>, mountPoint);
}
}
});
document.registerElement('x-search', {prototype: proto}); I’m closing because it is not actionable for React team. You might want to report this to Babel. |
Babel's class transform does not support extending native types: https://babeljs.io/docs/usage/caveats/#classes |
I was also following the examples from https://developers.google.com/web/fundamentals/getting-started/primers/customelements. This was where I was using the syntax for extending the native types; maybe I need a newer version of babel? For example, here's a snippet from the article: class AppDrawer extends HTMLElement {...}
window.customElements.define('app-drawer', AppDrawer); |
As @Kovensky wrote just above, Babel class transform just doesn't support extending built-ins such as |
|
Seems like I am bringing something back from the dead 😅. @gaearon any idea how can I disable class transforming in Babel, when I am using only the
|
If you only use the React preset Babel won't transform classes. Unless there's some other Babel config (in parent directories or e.g. webpack options). |
In my case, it's not related to babel or react. It's caused by chrome extension. |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Using the component causes the following error:
ReactDOMComponent.js:512 Uncaught TypeError: Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a function.(…)
Pasting the customElement definition into the console and calling document.createElement('orion-notification') creates an element. After loading react, it seems this error occurs.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/reactjs/69z2wepo/).
What is the expected behavior?
I should be able to instantiate the web component.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
Current react and current chrome.
The text was updated successfully, but these errors were encountered: