neo*_*ern 5 react-router redux react-router-redux
我正在尝试在分派操作时更改URL参数。它链接到实验中的下一个“案例”,并在URL中以整数形式显示。
我如何想象:
初始URL /experiment/2
分派操作{ type: 'NEXT_CASE' }
新URL/experiment/3
研究
基于此答案(尽管有关于redux-router),我想到可以通过访问还原器中的当前路径/ URL store.getState().routing。
但是我想要在路由中定义和解析的参数。(显然,我可以从路径中手动解析它们,但是如果路径结构发生更改,那似乎是不可扩展的,也不是面向未来的。)
当前解决方案
到目前为止,我基于匹配的最佳尝试(他们说这不适合客户端使用):
export default store => next => (action:Action<any>) => {
if (action.type == Types.NEXT_CASE) {
match(
{ routes: Routes, location: store.getState().routing.locationBeforeTransitions.pathname },
(error, redirectLocation, renderProps: any) => browserHistory.push(RouteBuilder.experiment(parseInt(renderProps.params.caseIndex) + 1))
);
}
return next(action);
}
Run Code Online (Sandbox Code Playgroud)
其中Routes和RouteBuilder定义如下:
export const RouteBuilder = {
experiment: (caseIndex: number) => `/experiment/${ caseIndex }`
};
export const Routes = (
<Route path="/" component={ Root }>
<IndexRoute component={ Welcome } />
<Route path="experiment/:caseIndex" component={ Experiment } />
</Route>
);
Run Code Online (Sandbox Code Playgroud)
问题
是否有任何简单的方法可在reducer中检索当前路线的(已解析和命名的)参数?
| 归档时间: |
|
| 查看次数: |
733 次 |
| 最近记录: |