包装您的组件
withRouter应该为您完成工作。
withRouter使用
link或其他任何
Router道具但不
Routerprops直接从
Route或从那里获得道具的组件需要
Parent Component
路由器道具可在组件调用时使用
<Route component={App}/>要么
<Route render={(props) => <App {...props}/>}/>或者如果您将
links路由器标记的直接子代放置为
<Router> <link path="/">Home</link></Router>
如果您希望将子内容作为组件写入Router中,例如
<Router> <App/></Router>
Router道具对App不可用,因此,您可以使用类似
<Router> <Route component={App}/></Router>但是,当您要为高度嵌套的组件提供Router道具时,withRouter会派上用场。检查此解决方案
import {withRouter} from 'react-router'class AppComponent extends React.Component { render() { return ( <div className="index"> <Nav /> <div className="container"> <Routes /> </div> </div> ); }}AppComponent.defaultProps = {};export default withRouter(AppComponent);


