简体   繁体   English

将特定的 React 组件导出到现有的 HTML/JS 项目

[英]Export specific React component to an existing legacy HTML/JS project

I have a React app, and I want to use one of the components in this React app in an existing legacy (and really old) web page (which is not using any JS framework).我有一个 React 应用程序,我想在现有的遗留(并且非常旧)网页(不使用任何 JS 框架)中使用这个 React 应用程序中的一个组件。

What I tried to do is actually a little messy and still didn't work:我试图做的实际上有点乱,但仍然不起作用:

Created a simple component (the one I want to show in my legacy app):创建了一个简单的组件(我想在我的旧应用中展示的那个):

import React from 'react';

const topUserTable = () => {
  return <h1>Top Users</h1>
};

export default topUserTable;

And in my App.js:在我的 App.js 中:

const TopUserTable = React.lazy(() => import(/* webpackChunkName: "TopUserTable" */"../components/TopUserTable"));

My webpack output covnfig:我的 webpack 输出 covnfig:

output: {
    path: path.join(__dirname, 'dist'),
    publicPath: 'assets/dist/',
    filename: 'assets/[name].[hash].js',
    chunkFilename: 'assets/[name].[chunkhash].js'
  },

and I ended up with a JS file named after my component in my dist folder:我最终在我的 dist 文件夹中得到了一个以我的组件命名的 JS 文件:

TopUserTable.1ebb44d2d4e148b3973e.js

The content of the file (I don't think it matters but for sanity)文件的内容(我认为这并不重要,但为了理智)

(window.webpackJsonp=window.webpackJsonp||[]).push([[3],{"./src/components/TopUserTable.js":function(e,n,s){"use strict";s.r(n);var o=s("./node_modules/react/index.js"),r=s.n(o);n.default=function(){return r.a.createElement("h1",null,"Top Users")}}}]);
//# sourceMappingURL=TopUserTable.1ebb44d2d4e148b3973e.js.map

So in my legacy project I did import to this file and to React:所以在我的遗留项目中,我确实导入了这个文件和 React:

<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script type="text/javascript" src="assets/TopUserTable.1ebb44d2d4e148b3973e.js"></script>

But using the tag shows nothing:但是使用标签什么也没显示:

<TopUserTable/>

Not sure what I'm missing or how I could do this better.不确定我缺少什么或我如何能做得更好。 My goal is simple:我的目标很简单:

  1. show an individual React component inside legacy web app OR在旧版 Web 应用程序中显示单个 React 组件或
  2. Import all my react app to my legacy project and use all the components anywhere将我所有的 react 应用程序导入到我的旧项目中,并在任何地方使用所有组件

How I can show my React component in my legacy app?如何在我的旧应用中显示我的 React 组件?

Thanks谢谢

I didn't see your DOM Container to the HTML of your page and a call to ReactDOM.我没有看到你的 DOM 容器到你的页面的 HTML 和对 ReactDOM 的调用。

You need in your legacy page (the same where you add the link tags)您需要在旧页面中(与添加链接标签的位置相同)

<div id="react_app"></div>

and in app.js并在 app.js 中

const domContainer = document.querySelector('#react_app');
ReactDOM.render(e(TopUserTable), domContainer);

note : e is actually const e = React.createElement;注意:e 实际上是const e = React.createElement;

more info here更多信息在这里

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

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