[英]React.js client route match giving “Invalid prop `routes` supplied to `Router`.”
Compiling my packages with webpack (there is a separate vendor.js and then an app.js, vendor.js just requires some basics like react, etc). 使用webpack编译我的软件包(有一个单独的vendor.js,然后是app.js,vendor.js仅需要一些基本知识,例如react等)。 I'm wondering why when I use {...renderProps} format in the Router declaration, that it throws this error.
我想知道为什么在路由器声明中使用{... renderProps}格式时会引发此错误。 If I change {...renderProps} to the standard routes={routes} format, it seems to work, but everyone tends to use {...renderProps}, and I'm trying to figure out why it isn't working for me.
如果我将{... renderProps}更改为标准route = {routes}格式,则似乎可以使用,但是每个人都倾向于使用{... renderProps},因此我试图弄清为什么它不起作用为了我。 I believe the dot alias {...x} is a stage-0 feature, so I have stage-0 defined in my webpack presets, but doesn't seem to affect the outcome.
我相信点别名{... x}是阶段0的功能,因此我在webpack预设中定义了阶段0,但似乎并不影响结果。 Here's my files:
这是我的文件:
app.js: app.js:
import React from 'react';
import { render } from 'react-dom';
import { match, Router, RouterContext, browserHistory } from 'react-router'
import { createHistory } from 'history'
import routes from './components/routes/AppRoute.jsx';
const { pathname, search, hash } = window.location;
const location = pathname;
match({ routes, location }, (error, redirectLocation, renderProps) => {
render(
<Router
{...renderProps}
history={browserHistory}
/>,
document.getElementById('react-app')
);
})
/components/routes/AppRoute.jsx is: /components/routes/AppRoute.jsx是:
import AppShell from '../AppShell.jsx';
import Index from '../Index.jsx';
if(typeof require.ensure !== "function") require.ensure = function(d, c) { c(require) }
module.exports = {
path: '/',
component: AppShell,
indexRoute: Index,
childRoutes: [
{ path: 'test',
getComponent(location, cb) {
require.ensure([], (require) => {
cb(null, require('../Test.jsx'))
})
}
},
{ path: 'user',
getComponent(location, cb) {
require.ensure([], (require) => {
cb(null, require('../User.jsx'))
})
}
},
{ path: '*',
getComponent(location, cb) {
require.ensure([], (require) => {
cb(null, require('../NotFound.jsx'))
})
}
},
{ path: 'app-shell', component: AppShell }
]
}
This seems to be exported correctly and being imported correctly into app.js, as console.log shows the object as expected. 这似乎已正确导出,并已正确导入到app.js中,因为console.log按预期显示了该对象。
But for some reason upon page load, the browser gives: 但是出于某种原因,页面加载后,浏览器会给出:
dll.vendor.js:330 Warning: Failed prop type: Invalid prop `routes` supplied to `Router`.
in Router
This error is being displayed from the dll.vendor.js file (not app.js), but I'm not sure that matters as I react is loaded into the vendors file, and react is responsible for showing the errors. 该错误是从dll.vendor.js文件(不是app.js)显示的,但是我不确定这是很重要的,因为我将React加载到了vendors文件中,而react负责显示错误。
Anyone have an idea as to why? 有人知道为什么吗?
Does changing the match
in app.js
from <Router>
to <RouterContext>
and deleting the history prop fix it? 将
app.js
的match
从<Router>
更改为<RouterContext>
并删除历史记录道具是否可以解决问题?
match({ routes, location }, (error, redirectLocation, renderProps) => {
render(
<RouterContext
{...renderProps}
/>,
document.getElementById('react-app')
);
})
That's what is recommended in react-router's docs 那是在React-Router的文档中推荐的
It is because the Router
component does not take routes
as a prop. 这是因为
Router
组件不将routes
作为支持。
As this is for server-side rendering, the code need to use RouterContext
rather than Router
. 因为这是用于服务器端渲染,所以代码需要使用
RouterContext
而不是Router
。
match
and Router
has been removed to make life simpler and been replaced with MemoryRouter
, BrowserRouter
and ServerRouter
. match
和Router
被删除以简化生活,并被MemoryRouter
, BrowserRouter
和ServerRouter
取代。
/* eslint-disable no-param-reassign */
import React from 'react';
import { renderToString } from 'react-dom/server';
import { ServerRouter/* , createServerRenderContext */ } from 'react-router';
// todo : remove line when this PR is live
// https://github.com/ReactTraining/react-router/pull/3820
import createServerRenderContext from 'react-router/createServerRenderContext';
import { makeRoutes } from '../../app/routes';
const createMarkup = (req, context) => renderToString(
<ServerRouter location={req.url} context={context} >
{makeRoutes()}
</ServerRouter>
);
const setRouterContext = (req, res, next) => {
const context = createServerRenderContext();
const markup = createMarkup(req, context);
const result = context.getResult();
if (result.redirect) {
res.redirect(301, result.redirect.pathname + result.redirect.search);
} else {
res.status(result.missed ? 404 : 200);
res.routerContext = (result.missed) ? createMarkup(req, context) : markup;
next();
}
};
export default setRouterContext;
for a working demo take a look at react-lego 工作演示,请看一下react-lego
ah, ok - the confusion was around the use of reactRouter.match
and RouterContext
both of which I believe are for server-side rendering only. 嗯,好的-混乱之处在于对
reactRouter.match
和RouterContext
的使用,我认为这两者仅用于服务器端渲染。
You should have a Router
, which takes children and a history (browser or memory). 您应该有一个
Router
,它带有孩子和一个历史记录(浏览器或内存)。
ReactDOM.render(
<Router history={history} >
<Route path="/" component={ MainLayout }>
<IndexRoute { ...indexRoute(routes.homepage) } />
<Route { ...routes.game } />
<Route { ...routes.notFound } />
</Route>
</Router>,
document.getElementById('html'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.