gpt4 book ai didi

reactjs - React Router/Hapi 服务器端渲染错误

转载 作者:行者123 更新时间:2023-12-03 13:40:01 30 4
gpt4 key购买 nike

我已经为此苦苦挣扎了一段时间,之前也能正常工作,但莫名其妙地,它又坏了,所以显然根本原因尚未解决。

react 路由器版本:2.0.0-rc4

问题:加载页面时,服务器返回以下错误。

Warning: Failed propType: Required prop `router` was not specified in `RouterContext`.
Warning: Failed propType: Required prop `location` was not specified in `RouterContext`.
Warning: Failed propType: Required prop `routes` was not specified in `RouterContext`.
Warning: Failed propType: Required prop `params` was not specified in `RouterContext`.
Warning: Failed propType: Required prop `components` was not specified in `RouterContext`.
Warning: [react-router] `<RouterContext>` expects a `router` rather than a `history`

页面加载正常,并且客户端路由似乎工作得很好。

来自 Server.js 的相关片段:

import routeConfig from './../common/routes/Routes.js';

const handleRender = function(req, res) {
const initialState = {
profile: {
name: 'Bob',
age: 10
},
messages: []
}
const createStoreWithMiddleware = applyMiddleware( thunkMiddleware)(createStore);
const store = createStoreWithMiddleware(reducer(initialState));


match({routes: routeConfig, location: req.url}, (error, redirectLocation, renderProps) => {
// res(req.url);
if(error) {
res('error' + error.message);
}
else {
res(renderProps);
const html = renderToString(
<Provider store={store}>
<RouterContext {...renderProps} />
</Provider>
);

//const initialState = store.getState();

//res(renderFullPage(html, initialState));
}
});
}

这是从 Routes.js 导出的内容

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import { Route } from 'react-router';

//Components
import App from './../components/App.jsx';
import Name from './../components/Name.jsx';
import Profile from './../components/Profile.jsx';
import Messages from './../components/Messages.jsx';

const routeConfig = [
{
path: '/',
component: App,
childRoutes: [
{path: 'test', component: Name},
{path: 'profile', component: Profile},
{path: 'messages', component: Messages}
]
}
];

export default routeConfig;

当我转储渲染属性时,这就是我得到的

{
routes: [
{
path: "/",
childRoutes: [
{
path: "test"
},
{
path: "profile"
},
{
path: "messages"
}
]
},
{
path: "test"
}
],
params: { },
location: {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: { },
pathname: "/test",
path: "/test",
href: "/test"
},
components: [
null,
null
],
history: {
__v2_compatible__: true
},
router: {
__v2_compatible__: true
}
}

所以看起来它永远不会匹配组件。也许我错误地传递了 req.url ?但我找不到任何反应路由器文档来准确指示该参数应该是什么样子。

最佳答案

将其留在这里,以防其他人遇到这种愚蠢的事情。

通过 Good 启用更强大的错误日志记录后,我意识到此错误实际上是引用/favicon.ico 的请求,我的路由未处理该请求,并且该请求落入了我的 React 路由中。

我犯了一个非常愚蠢的错误,因为我缺乏在 Hapi 中处理/记录错误的经验。

关于reactjs - React Router/Hapi 服务器端渲染错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34592590/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com