之前一直用的是hashHistory
,但以后升级react-router v4
之后hashHistory
被移除了,我知道可以用 yield put(routerRedux.push('xxx'))
,不过我的场景是在effects里面的回调跳转,通过这个issue,我知道了这种写法可能不好,但我也不知道该怎么弄,场景如下:
- 页面初始化发出一个验证请求
- 得到请求结果
- 如果验证不通过,弹出一个提示框
- 点击确认,页面跳转
伪代码:
effects: {
*init({}, {call, put}) {
const result = yield call('xxx');
if (!result) {
Modal.confirm({
title: '是否跳转?',
onOk() {
// 这里做跳转
}
});
}
}
在onOk
里面不能用routerRedux跳转,这也不是从组件内用dispatch
过来的方法可以把回调传进来,这种情况该怎么处理呢?
把这段代码
封装到
Promise
中,然后通过
call
来调用。