【发布时间】:2017-01-23 02:51:38
【问题描述】:
用 webpack 编译我的包(有一个单独的 vendor.js,然后是一个 app.js,vendor.js 只需要一些基础知识,比如 react 等)。我想知道为什么当我在 Router 声明中使用 {...renderProps} 格式时,它会引发此错误。如果我将 {...renderProps} 更改为标准 routes={routes} 格式,它似乎可以工作,但每个人都倾向于使用 {...renderProps},我试图找出它为什么不起作用为了我。我相信点别名 {...x} 是一个阶段 0 功能,所以我在我的 webpack 预设中定义了阶段 0,但似乎不会影响结果。这是我的文件:
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 是:
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 }
]
}
这似乎已正确导出并正确导入到 app.js 中,因为 console.log 显示了预期的对象。
但由于某些原因在页面加载时,浏览器给出:
dll.vendor.js:330 Warning: Failed prop type: Invalid prop `routes` supplied to `Router`.
in Router
这个错误是从 dll.vendor.js 文件(不是 app.js)中显示出来的,但我不确定这是否重要,因为我的 react 已加载到供应商文件中,并且 react 负责显示错误。
有人知道为什么吗?
【问题讨论】:
标签: reactjs webpack react-router