访问 React 路由中的 Router.history 属性



我的路由定义如下:

<Router history={hashHistory}>
    <Route path="/" component={MainView}>
        <IndexRoute component={SearchBarMainView} />
        <Route path="search/:keyword" component={SearchResultsMainView}/>
        <Route path="*" component={NotFound} />
    </Route>
</Router>

如何从组件访问路由器的历史记录属性?我需要使用它来使用history.push(url) - 无论我使用 hashHistory 还是browserHistory - 换句话说,我希望我的应用程序与历史类型无关,并使用提供给路由器的历史对象

将组件包装在withRouter高阶组件中。这会将router道具注入到组件中,您可以使用该道具进行导航。

最新更新