[英]Warning: Failed prop type: Invalid prop `component` supplied to `Route`. & Warning: [react-router] Location “/” did not match any routes
I speak little engilsh,sorry first. 我会说一点小礼,抱歉。
I have searched some similar questions, but not help. 我搜索了一些类似的问题,但没有帮助。 Can't figure out what happen by myself, it almost spend me 1 hours.
我不知道自己会发生什么,几乎花了我一个小时。
I start my project with latest yeoman & generator-react-webpack. 我从最新的yeoman和generator-react-webpack开始我的项目。
Some error always exist.In the browser console. 在浏览器控制台中总是存在一些错误。
Warning: Failed prop type: Invalid prop `children` supplied to`Router`.
Warning: [react-router] Location "/" did not match any routes
here is index.js
. 这是
index.js
。
import 'core-js/fn/object/assign';
import React from 'react';
import ReactDOM from 'react-dom';
import {Router, Route, hashHistory, IndexRoute} from 'react-router';
import IndexPage from './components/IndexPage';
import Pages from './components/Pages';
ReactDOM.render((
<Router history={hashHistory}>
<Route path="/" component={IndexPage} />
<Route path="/pages" component={Pages} />
</Router>
), document.getElementById('webapp'));
here is Indexpage.js
. 这是
Indexpage.js
。
import React, {Component} from 'react';
export default class IndexPage extends Component {
render() {
return (
<div>1234</div>
)
}
}
and here is pages.js
. 这是
pages.js
。
import React, {Component} from 'react';
export default class Pages extends Component {
render() {
return (
<div>9876</div>
)
}
}
Checkout if the filenames and import statements have the correct spelling. 检查文件名和导入语句的拼写是否正确。
pages.js should be P ages.js pages.js应该是P ages.js
Indexpage.js should be Index P age.js Indexpage.js应为Index P age.js
Or change it in the import statements.. its case-sensitive. 或在import语句中更改它。区分大小写。
The docs says you have to wrap your Routes with a main Router / 文档说您必须使用主路由器/来包装路由
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="about" component={About}/>
<Route path="users" component={Users}>
<Route path="/user/:userId" component={User}/>
</Route>
<Route path="*" component={NoMatch}/>
</Route>
</Router>
// this is the main App component
const App = ({children}) => (
<div id="main-layout">{children}</div>
)
Your pages.js has a wrong class name. 您的pages.js的类名错误。 Instead of also calling it as IndexPage, you want to call it as Pages like below.
与其将其称为IndexPage,还不如将其称为Pages,如下所示。
import React, {Component} from 'react';
export default class Pages extends Component {
render() {
return (
<div>9876</div>
)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.