reactjs - 不变违规:浏览器历史记录需要一个 DOM
问题描述
对于服务器端,BrowserRouter 将不起作用,因此将按照文档使用 StaticRouter 。我也在做同样的事情,但我仍然收到错误消息。以下是我的设置
不变违规:浏览器历史记录需要一个 DOM
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { StaticRouter as Router } from 'react-router';
// import our main App component
import App from '../src/App';
const path = require('path');
const fs = require('fs');
export default (req, res) => {
// get the html file created by CRA's build tool
console.log('url : ', req.baseUrl);
const filePath = path.resolve(__dirname, '..', '..', 'build', 'index.html');
fs.readFile(filePath, 'utf8', (err, htmlData) => {
if (err) {
console.error('err', err);
return res.status(404).end();
}
// render the app as a string
const html = ReactDOMServer
.renderToString(<Router location={req.baseUrl}>
<App />
</Router>);
// now inject the rendered app into our html and send it
return res.send(htmlData
.replace('<div id="root"></div>', `<div id="root">${html}</div>`));
});
};
解决方案
推荐阅读
- mongodb - mongdb 中的聚合 addFields 和 not_In 出错
- python - 是否有用于 ANOVA 事后分析的 Games-Howell 多重比较的 python 包?
- firebase - 如何在 firebase 中使用 Cloud Functions 更新使用 Cloud Firestore 中的数据填充的 UI 元素?
- kubeflow-pipelines - 如何使用 Kubeflow Pipelines API 进行身份验证
- php - 我想在 Laravel 集合中返回一些键和值
- javascript - handlebarsjs 通过数字 id 访问上下文
- c++ - (C++) 中断;并返回;不适用于我的 for() 函数
- boolean-logic - 表达式 !A + (A . !B) = !(AB) 如何?
- php - 如何在搜索结果 Wordpress 中启用 TablePress 内容
- javascript - 使父级 div 高度与子级相同