浏览到 baseURL/index.html 会呈现一个空的 <div>。
默认情况下,CRA 构建 index.html,没有可见的 <body> 内容。它只包含一个空的“根”div。这是代码:
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
直接浏览到index.html 会呈现该页面,显示空 div。这就是为什么您什么也看不到,或者说“灰屏”。
浏览到 baseURL 允许 React 运行。
浏览 React 项目的 baseURL 可以让 React 的魔法发挥作用。文件index.js 被处理,React 将你的<App /> 组件插入到index.html 的空“根” div 中。
React render 函数执行此任务。
ReactDOM.render() 函数有两个参数,HTML 代码和一个
HTML 元素。该函数的目的是显示指定的
指定 HTML 元素内的 HTML 代码。
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
在上面的render 函数中,第一个参数(<App /> 组件的输出)插入到第二个参数(index.html 中的“根”div)中标识的元素中。结果,浏览器会显示您的应用程序输出。