generator-spfx icon indicating copy to clipboard operation
generator-spfx copied to clipboard

Implementation of Web Component Wrapper for React

Open StfBauer opened this issue 5 years ago • 3 comments

General Information

  • [ ] Usage
  • [ ] Development
  • [ ] Documentation
  • [x] Feature Request

Describe your Request

Add support for web components to ReactJS to projects: https://reactjs.org/docs/web-components.html

StfBauer avatar Jul 04 '19 15:07 StfBauer

What's your vision behind? web components can be made with react / angular / plain javascript

petkir avatar Jan 21 '20 18:01 petkir

@petkir haven't looked into it but if you like to look into you are more than welcome. The only web components-ish implementation are currently VueJS or Angular Elements.

StfBauer avatar Jan 21 '20 19:01 StfBauer

Angular Elements is really comfortable for developers. I have only written my own wrapper for React Components.

class MyReactWrapper extends HTMLElement {
  reactMountPoint;
  componentAttributes = {};
  connectedCallback() {
    this.mountReactApp();
  }
  disconnectedCallback() {
    ReactDOM.unmountComponentAtNode(this.reactMountPoint);
  }
  static get observedAttributes() {
    return ['prop1', 'prop2'];
  }
  attributeChangedCallback(name, oldVal, newVal) {
    this.componentAttributes[name] = newVal;
    this.mountReactApp();
  }
  mountReactApp() {
    if (!this.reactMountPoint) {
      this.mountPoint = document.createElement('div');
      this.attachShadow({ mode: 'open' }).appendChild(this.reactMountPoint);
    }
    ReactDOM.render(<MyReact { ...this.componentAttributes } />, this.reactMountPoint);
  }
}
window.customElements.define('my-react', MyReactWrapper);

Eg.: https://github.com/LukasBombach/react-web-component

But the Main Source in SPfx is written in React what's the use case:

  • include React Web-Components in React
  • include React Web-Components in Angular or Vue (Maybe for Office UI )

petkir avatar Jan 21 '20 20:01 petkir