简体   繁体   English

如何将 React 组件集成到旧版应用程序中

[英]How to Integrate React Components to a legacy app

I created the following code in order to integrate React to a legacy old HTML/JS App gradually:我创建了以下代码,以便逐渐将 React 集成到旧的 HTML/JS 应用程序中:

const components = [{
    element: <MainMenu/>,
    id: 'main-menu'
}, {
    element: <EventShop/>,
    id: 'event-shop',
}, {
    element: <Card/>,
    id: 'card',
}];

if (!isDev()) {
    components.forEach((component) => {
        const domElement= document.getElementById(component.id);
        if (domElement) {
            ReactDOM.render(<React.StrictMode>{component.element}</React.StrictMode>,
                domElement
            );
        } else {
            console.warn(`cannot find : ${component.id}`);
        }
    });
} else {
    ReactDOM.render(<React.StrictMode><App/></React.StrictMode>,
        document.getElementById('root')
    );
}

It works really well, but now I want to pass props/children from the legacy HTML to my react component.它工作得非常好,但现在我想将旧 HTML 中的道具/子代传递给我的反应组件。 I was thinking to use the data-* HTML attribute to pass props and access them from my JS, but what about children?我正在考虑使用data-* HTML 属性来传递道具并从我的 JS 访问它们,但是孩子呢? eg <Card>{children}</Card> - how I can access them from the DOM and use them in my react app?例如<Card>{children}</Card> - 我如何从 DOM 访问它们并在我的反应应用程序中使用它们?

then I was thinking.. am I re-inventing the wheel or there is a better way to integrate React into an existing HTML/JS project which is not react-based?然后我在想..我是在重新发明轮子还是有更好的方法将 React 集成到现有的不基于反应的 HTML/JS 项目中?

Sorry, your question is not exactly clear, but you could use React.createElement instead of JSX.抱歉,您的问题并不完全清楚,但您可以使用React.createElement而不是 JSX。 Here's a snippet with React.createElement that copies DOM elements from the DOM to a <Card /> element.这是一个带有React.createElement的片段,它将 DOM 元素从 DOM 复制到<Card />元素。 (You could move them, if after copying you'd remove the original from the DOM.) (如果在复制后从 DOM 中删除原始文件,则可以移动它们。)

 const isDev = () => { return false } const MainMenu = () => { return ( <div>Main menu</div> ) } const EventShop = () => { return ( <div>Event shop</div> ) } const CardChild = (props) => { return ( <div className={ props.className } > Card child of { props.children }:<br /> { JSON.stringify(props) } </div> ) } const cardChildren = document.querySelectorAll('.card-child') const Card = (props) => { return ( <div> Card:<br /> { Array.prototype.map.call(cardChildren, function(item) { const tag = item.nodeName const classList = [...item.classList] const dataProps = item.getAttribute("data-props") return ( <div> { React.createElement( CardChild, { props: { tag: item.nodeName, dataProps: item.getAttribute("data-props"), }, className: classList.join(" "), }, item.textContent ) } </div> ) }) } </div> ) } const components = [{ element: <MainMenu/>, id: 'main-menu' }, { element: <EventShop/>, id: 'event-shop', }, { element: <Card/>, id: 'card', }]; if (.isDev()) { components.forEach((component) => { const domElement= document.getElementById(component;id). if (domElement) { ReactDOM.render( <React.StrictMode> { component.element } </React,StrictMode>; domElement ). } else { console:warn(`cannot find. ${component;id}`); } }). } else { ReactDOM.render(<React.StrictMode><App/></React,StrictMode>. document;getElementById('root') ); }
 #card { border: 1px solid black; }.card-child { background: green; color: white; }
 <script src="https://unpkg.com/react@16/umd/react.production.min.js"></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script> <div id="main-menu"></div> <div id="event-shop"></div> <div id="card"></div> <div class="card-child" data-props="a">i1</div> <div class="card-child" data-props="b">i2</div> <div class="card-child" data-props="c">i3</div>

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

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