在4.0如下的react router中,嵌套的路由能夠放在一個router標籤中,形式以下,嵌套的路由也直接放在一塊兒。php
<Route component={App}> <Route path="groups" components={Groups} /> <Route path="users" components={Users}> <Route path="users/:userId" component={Profile} /> </Route> </Route>
可是在4.0之後,嵌套的路由與以前的就徹底不一樣了,須要單獨放置在嵌套的根component中去處理路由,不然會一直有warning:css
You should not use <Route component> and <Route children> in the same routehtml
正確形式以下java
<Route component={App}> <Route path="groups" components={Groups} /> <Route path="users" components={Users}> //<Route path="users/:userId" component={Profile} /> </Route> </Route>
上面將嵌套的路由註釋掉react
const Users = ({ match }) => ( <div> <h2>Topics</h2> <Route path={`${match.url}/:userId`} component={Profile}/> </div> )
上面在須要嵌套路由的component中添加新的路由nginx
import React, { Component } from 'react'; import ReactDOM from 'react-dom'; // import { Router, Route, Link, Switch } from 'react-router'; import { HashRouter, Route, Link, Switch } from 'react-router-dom'; class App extends Component { render() { return ( <div> <h1>App</h1> <ul> <li><Link to="/">Home</Link></li> <li><Link to="/about">About</Link></li> <li><Link to="/inbox">Inbox</Link></li> </ul> {this.props.children} </div> ); } } const About = () => ( <div> <h3>About</h3> </div> ) const Home = () => ( <div> <h3>Home</h3> </div> ) const Message = ({ match }) => ( <div> <h3>new messages</h3> <h3>{match.params.id}</h3> </div> ) const Inbox = ({ match }) => ( <div> <h2>Topics</h2> <Route path={`${match.url}/messages/:id`} component={Message}/> </div> ) ReactDOM.render( (<HashRouter> <App> <Route exact path="/" component={Home} /> <Route path="/about" component={About} /> <Route path="/inbox" component={Inbox} /> </App> </HashRouter>), document.getElementById('root') );