简体   繁体   English

使用 React 和 Web 组件

[英]Using React and Web Components

In our project we are using React and Web Components to develop reusable UI components (which in turn will be used by various dev teams internally).在我们的项目中,我们使用 React 和 Web Components 来开发可重用的 UI 组件(反过来,这些组件将被内部的各种开发团队使用)。 Components are developed in React and registered as custom HTML elements through Web Components.组件在 React 中开发并通过 Web Components 注册为自定义 HTML 元素。 We need a way through which we can define the props in HTML custom tag and access all the props in our React Component.我们需要一种方法来定义 HTML 自定义标签中的 props 并访问 React 组件中的所有 props。

The HTML would be like HTML 会像

<custom-element props1='pageInfo' props2='mediaInfo'></custom-element>

pageInfo and mediaInfo will be JS Objects which will be declared in global window scope or they can be inside some other NameSpace/Object, in that case the HTML would be something like pageInfomediaInfo将是 JS 对象,它们将在全局窗口范围内声明,或者它们可以在其他一些命名空间/对象内,在这种情况下,HTML 将类似于

<custom-element props1='NS.pageInfo' props2='NS.mediaInfo'></custom-element>

OR或者

<custom-element props1='NS.Page.pageInfo' props2='NS.Media.mediaInfo'></custom-element>

So, we need a way to get all the props defined in the HTML and resolve them as Objects and pass it on to ReactDOM.render因此,我们需要一种方法来获取 HTML 中定义的所有道具并将它们解析为对象并将其传递给ReactDOM.render

Currently the code to render and register custom element is,目前呈现和注册自定义元素的代码是,

class RegComponent extends HTMLElement {
    constructor() {
        super();
    }
    createdCallback() {
        ReactDOM.render(<App props1={eval(this.getAttributes('props1'))}/>, this);
    }
}
document.registerElement('custom-element', RegComponent);

We want to get rid of eval and all the declared props should be fetched from HTML and passed on to ReactDOM.render .我们想要摆脱 eval 并且所有声明的 props 都应该从 HTML 中获取并传递给ReactDOM.render Looking for something like,寻找类似的东西,

ReactDOM.render(<App {getAllProps()}/>, this);

where getAllProps() should return all the props name & their value.其中getAllProps()应返回所有道具名称及其值。 Remember that I'm using ES6.请记住,我使用的是 ES6。 Any help would be appreciated!任何帮助,将不胜感激!

What about instead of using JSX:不使用 JSX 怎么样:

ReactDOM.render(<App props1={eval(this.getAttributes('props1'))}/>, this);

Use React directly, with an adapter transforming attributes into props:直接使用 React,通过适配器将属性转换为 props:

ReactDOM.render(React.createElement(App, {...getAllProps(this.attributes)}), this);

function getAllProps(attributes) {
    var props = {};
    for (var i = 0; i < attributes.length; i++) {
        props[attributes[i].nodeName] = attributes[i].nodeValue;
    }
    return props;
}

If getAllProps() returns an object, and each property in the object is the prop you wanted, you should only need to update you render to use the spread operator( ... ).如果getAllProps()返回一个对象,并且该对象中的每个属性都是您想要的道具,您应该只需要更新您的渲染以使用扩展运算符 ( ... )。 This will deconstruct your object so that each property is passed to the App as a prop .这将解构您的对象,以便将每个属性作为prop传递给App

Here is what it would look like:这是它的样子:

ReactDOM.render(<App {...getAllProps()}/>, this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM