简体   繁体   English

如何将 React 应用程序添加到现有网站

[英]How to add a React app to an existing website

I create a small React application using npx create-react-app my-app .我使用npx create-react-app my-app创建了一个小型 React 应用程序。 I would like to add this React application to my existing website in a specific <div> .我想将此 React 应用程序添加到我现有网站的特定<div>中。

My website: <div id="reactApp"></div>我的网站: <div id="reactApp"></div>

In React, I have index.js as在 React 中,我有 index.js 作为

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  //document.getElementById('root')
  document.getElementById('reactApp')
);

reportWebVitals();

Then, I build the application npm run build .然后,我构建应用程序npm run build It generates the following files (image below) in static folder.它在 static 文件夹中生成以下文件(下图)。 Do I need to add all these.css and.js files to my main website to make it work?我是否需要将所有这些.css 和.js 文件添加到我的主网站以使其正常工作?

在此处输入图像描述

Convert your project my-app to a web component and then add it to the other react application (you can add it to any other web application).将您的项目 my-app 转换为 web 组件,然后将其添加到其他 React 应用程序(您可以将其添加到任何其他 web 应用程序)。

More details on how to create web components can be found here https://blog.bitsrc.io/web-component-why-you-should-stick-to-react-c56d879a30e1有关如何创建 web 组件的更多详细信息,请参见https://blog.bitsrc.io/web-component-why-you-should-stick-to-react-c56d879a30e1

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

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