Dva: dispatch.().then() 在组件加载前或后执行会抛出dispatch(...).then is not a function错误

Created on 4 Jan 2018  ·  5Comments  ·  Source: dvajs/dva

Code to reproduce the issue: (请提供可复现的代码或者步骤)

componentWill(Did)Mount(){
const {dispatch}=this.props;
dispatch({
type:"example/fetchCurrent"
}).then(()=>{
console.log("等他执行完执行")
})
}

Expected behavior: (预期的正常效果)

正常打印:等他执行完执行

Actual behavior: (实际效果)

报错:dispatch(...).then is not a function

Versions of packages used: (哪个库的哪个版本出现的问题)

dva2.1.0

经过测试:此次发起的dispatch一定要延时一定时间才会正常

Most helpful comment

@dsmelon dipatch 如果要返回一个 Promise 需要 action 为 effects,同步的 dispatch(action) 返回的为action对象。

All 5 comments

@dsmelon dipatch 如果要返回一个 Promise 需要 action 为 effects,同步的 dispatch(action) 返回的为action对象。

@yvanwangl 是effects,你可以自己测试一下。不然也不会出现延时一下就正常的问题

@dsmelon 你 dva 版本是多少的?我试了可以的

找到了正解,之前dispatch reducer里面的方法使用.then各种报错,找不到原因,原来只能dispatch effects才可以使用.then()

问题有人解决了吗?我也遇到一样的问题,不知哪里错了

Was this page helpful?
0 / 5 - 0 ratings