【发布时间】:2023-03-12 16:40:02
【问题描述】:
使用 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 生命周期方法对其进行更新。一定有更好的办法吗?
【问题讨论】:
标签: javascript reactjs redux react-router react-router-redux