如何添加新的html文件以响应应用程序?

时间:2019-08-04 16:37:03

标签: reactjs

我一直在互联网上搜索此问题,而我所得到的就是无需创建其他页面,因为react用于单页面应用程序 或更改页面内容(而不更改页面(URL)本身)的解决方案。 我真正想知道的是: 使用create-react-app创建应用程序时,我已完成所有设置,现在可以创建了 在公共目录中创建了一个新的html文件,如默认的index.html, 将组件渲染到该新文件?

2 个答案:

答案 0 :(得分:0)

CRA正在使用webpack从(默认) from random import random from math import floor def next_char2(c): if c not in rules: return c d = rules[c] r = floor(random() * len(d)) # was int(...) before # Rules start with key 1. # Random brings a float between 0 and 1, therefore you need [r + 1] as key return d[r + 1] In [6]: %timeit next_char("X") 3.42 µs ± 32.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) In [7]: %timeit next_char2("X") 814 ns ± 12.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) Edit: Changing the int with math.floor gives a little boost In [10]: %timeit next_char2("X") 740 ns ± 8.57 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) 文件开始捆绑您的代码。这意味着它将创建一个(或多个(如果是延迟加载的话))捆绑,然后将其捆绑到src/index.js文件中。您可以通过查看public/index.html并查找在build/index.html标签之前加载的脚本来进行检查。

React的主要入口是

</body>

一个HTML文件中可以有多个入口点。如果您想将多个文件分割成多个入口点-恐怕您需要ReactDOM.render(<App />, document.getElementById('root')); 自己的CRA应用程序并自行调整Webpack配置。

我认为这里描述了类似的问题: Multiple modules for multiple entry points in webpack config for reactjs app

但是也许您只是在寻找react-router?您还可以调整服务器配置,以将所有路径重定向到eject,并让react-router处理显示正确的组件。例如。 How to setup apache server for React route?

答案 1 :(得分:0)

根据您的问题,尚不清楚您需要多个HTML文件的确切原因是什么,但是请从CRA readME中考虑:

  

如果要使用React和Node.js进行服务器渲染,请查看Next.js或Razzle。 Create React App与后端无关,仅生成静态HTML / JS / CSS包。

     

如果您的网站大部分是静态的(例如,投资组合或博客),请考​​虑改用Gatsby。与Create React App不同,它在构建时会将网站预先呈现为HTML。

因此,我认为有必要了解whether it is possible to do this with CRA?而不是Why do you need it? And maybe there is a tool more suitable for your case than CRA.

也许next.jsrazzlegatsbyjs将解决您的问题。