acdlite / acdlite/redux-router

Infinite recursion when dispatching action in onEnter hook.

未关闭
#70 9 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
bug
主要语言
JavaScript
星标
2.3k
派生
194
PR 合并指标
30 天内没有已合并 PR

描述

To initially fill the state of my app on the server, components subscribe to the onEnter hook of their routes. In this static initialisation functions I dispatch some actions.

Now this code in redux-router kicks in:

https://github.com/rackt/redux-router/blob/master/src/client.js#L29

... and replaces the history again (calling onEnter again) ...

https://github.com/rackt/redux-router/blob/master/src/client.js#L37

... because it didn't had the chance to set `routerState` which initially is set to `undefined` ...

https://github.com/rackt/redux-router/blob/master/src/client.js#L14

... resulting in an infinite loop.

Just setting `routerState` before the `replaceHistory` call fixes the loop - but everything still gets called twice.

``` js
store.subscribe(() => {
const nextRouterState = routerStateSelector(store.getState());

if (
nextRouterState &&
!routerStateEquals(routerState, nextRouterState)
) {
const { state, pathname, query } = nextRouterState.location;
routerState = nextRouterState;
history.replaceState(state, pathname, query);
} else {
routerState = nextRouterState;
}
});
```

There has to be a way to initialise `routerState` with the correct state instead of `undefined` on the first call.

贡献指南

这个仓库没有索引到贡献指南

调研方向

The issue is in src/client.js lines 14, 29, and 37. Start by understanding the subscribe callback and how routerState is initialized. The fix involves setting routerState before replaceHistory to prevent the loop. Check the routerStateSelector and routerStateEquals functions. Test by simulating server-side rendering with onEnter hooks that dispatch actions.

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, react
领域
frontend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。