我在用react-router-dom
我猜测这是导致问题的原因,但我不知道从哪里开始寻找或如何解决它。我也遇到类似的错误Warning: Did not expect server HTML to contain a <nav> in <div>
.
正如我所说,我不太确定在哪里查找,所以如果您认为某些代码会有所帮助,请告诉我,我会发布它。否则,我可以发布我用来执行 SSR 的代码。
EDIT:确切的错误:Warning: Prop
hrefdid not match. Server: "/profile/5a073dc44cb45b00125e5c82" Client: "profile/5a073dc44cb45b00125e5c82"
我检查过客户端,它有/profile/:id
所以不确定哪里说没有/
,至于另一个错误<nav>
in <div>
, 我有一个nav
在我的 header 中,但我不太确定如何“修复”它。
import React from 'react';
import { renderToString } from 'react-dom/server';
import { StaticRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import { renderRoutes } from 'react-router-config';
import serialize from 'serialize-javascript';
import { Helmet } from 'react-helmet';
import { matchRoutes } from 'react-router-config';
import routes from './src/routes';
import createStore from './src/stores';
function handleRender(req, res) {
let initial = {};
if (req.vertexSession != null && req.vertexSession.user != null) {
initial.user = { currentUser: req.vertexSession.user };
}
const store = createStore.configure(initial); // create Store in order to get data from redux
const promises = matchRoutes(routes, req.path)
.map(({ route, match }) => {
// Matches the route and loads data if loadData function is there
return route.loadData
? route.loadData(store)
: route.loadDataWithMatch ? route.loadDataWithMatch(store, match) : null;
})
.map(promise => {
if (promise) {
return new Promise((resolve, reject) => {
promise.then(resolve).catch(resolve); // lets all data load even if route fails
});
}
});
Promise.all(promises).then(() => {
const context = {};
if (context.url) {
return res.redirect(301, context.url); // redirect for non auth users
}
if (context.notFound) {
res.status(404); // set status to 404 for unknown route
}
const content = renderToString(
<Provider store={store}>
<StaticRouter location={req.path} context={context}>
<div>{renderRoutes(routes)}</div>
</StaticRouter>
</Provider>
);
// console.log(store.getState());
const initialState = serialize(store.getState());
const helmet = Helmet.renderStatic();
res.render('index', { content, initialState, helmet });
});
}
module.exports = handleRender;
你已经解决这个问题了吗?我的反应应用程序也遇到了类似的问题并修复了它。这是我的问题:
<Link to="./shop">Shop</Link>
my fix:
<Link to="/shop">Shop</Link>
无论您使用服务器渲染什么,都是问题所在。我建议梳理一下你的路由模块,看看你是否在某处忘记了正斜杠。如果这不起作用,请查看您在路由文件中导入的组件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)