我已经开始学习 React 服务器端渲染。然后我配置“ index.js ”文件如下:
const express = require('express');
const app = express();
const React = require('react');
const renderToString = require('react-dom/server').renderToString;
const Home = require('./client/components/home').default;
app.get('/', (req, res) => {
const content = renderToString(<Home></Home>);
res.send(content); // Statement1
});
app.listen(3000, ()=> {
console.log("App listening to port 3000");
});
因此,在上面代码的 Statement1 中,我们从“Home”组件返回字符串(因为“renderToString”将 HTML 转换为字符串。
当我检查 DOM 时,我得到以下代码:
<html>
<head></head>
<body cz-shortcut-listen="true">
<div data-reactroot="">I'm home component</div>
</body>
</html>
所以,我的问题是浏览器如何将 HTML 字符串(作为服务器的响应接收)转换为 DOM 元素,以及如何<body>
添加这个“”标签?