简体   繁体   English

如何将 React 添加到已经构建的网站?

[英]How to add React to an already built website?

I already have a built website with Express and I wanted to use React just in some pages.我已经用 Express 建立了一个网站,我想在某些页面中使用 React。 How is the best way to add it having all the resources that I have using the create-react-app?使用 create-react-app 拥有的所有资源添加它的最佳方法是什么?

I know I can add each script to the HTML file, but that is kind of error prone and laborious.我知道我可以将每个脚本添加到 HTML 文件中,但这很容易出错且费力。 I just wanted to be able to do all the import and manage the files the same way I do with an application using create-react-app.我只是希望能够像使用 create-react-app 的应用程序一样执行所有导入和管理文件。

If you only want to use React in some already existing pages, I would suggest you to go through importing the script as React documentation talks about here .如果你只想在一些已经存在的页面中使用 React,我建议你通过导入脚本来 go,因为 React 文档在这里谈到了。 It gives you a nice and short example on how to do add react components to existing html pages.它为您提供了一个很好的简短示例,说明如何将反应组件添加到现有的 html 页面。 You only need to import the react and react-dom dependencies once in your html entry-point (probably index.html), then in each page you only import the required components.您只需在 html 入口点(可能是 index.html)中导入一次 react 和 react-dom 依赖项,然后在每个页面中只导入所需的组件。

The other alternative is to follow the idea in this guide , to build the app using both create-react-app and express.另一种选择是遵循本指南中的想法,使用 create-react-app 和 express 构建应用程序。 You would want to move all the html code into React components and handle everything from React, and you will be able to manage all the project structure like you wish.您可能希望将所有 html 代码移动到 React 组件中并处理来自 React 的所有内容,并且您将能够按照您的意愿管理所有项目结构。 But I believe this is more error prone and a lot of effort, if you only want to add React to build some componentes in just some pages.但是我相信如果你只想添加 React 来在一些页面中构建一些组件,这更容易出错并且需要付出很多努力。

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

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