如果路由更改,则更改 React 组件

Change React component if route changes

我正在使用 react-redux-router。 我有以下代码:

<Router history={hashHistory}>
    <Route path="/" component={App}>
        <IndexRoute component={Home} />
        <Route path="/product/:id" component={Product} />
    </Route>
</Router>

当我的路由是“/”和我的路由是“/product/:id”时,我想在 App 组件中显示不同的数据。如何跟踪组件应用程序中路由的变化。

使用 react-router-reduxsyncHistory 中间件来保存路由。然后你可以在 App 组件中使用 connectstate.routing.location.pathname 读取路径名并将其推送到 props.