使用 redux 和 react 路由器,我想访问路由指定组件以外的组件上的路由参数。
我已经按如下方式设置了 react-router、redux 和 react-router-redux,我想从我的 redux 存储中访问 reportId 参数。
const history = syncHistoryWithStore(browserHistory, store);
const store = createStore(reducers);
const routes = (
<Router history={history}>
<Route path="/" component={MainLayout} >
<IndexRoute component={Home} />
<Route path="reports">
<Route path=":reportId" component={ReportViewerContainer} />
</Route>
</Route>
</Router>
);
ReactDOM.render(
<Provider store={store}>{router}</Provider>,
document.getElementById('root')
);
我尝试连接 react-redux-router 来存储路由状态,但我发现除了事件路由的完整路径之外,redux 内部没有存储任何有用的东西。这让我可以选择从 redux 中的路径解析出 reportId,或者在 redux 中创建我自己的自定义操作并使用我的 ReportViewerContainer 组件内的 componentWillReceiveProps 生命周期方法更新它。一定有更好的方法吗?
最佳答案
如果你想访问组件内部的路由器参数,最好的方法是使用withRouter
通过上面的例子你会发现更好的理解。
关于javascript - Redux 中的 React Router 路由参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40372428/