Dva: 关于dispatch() .then用法的疑惑

Created on 19 Jul 2018  ·  7Comments  ·  Source: dvajs/dva

dispatch().then(),then的参数当中是否可以直接返回请求的结果,或者具体想知道then中是不是有默认返回的参数呢,还是直接是空的。

Most helpful comment

then中是不是有默认返回的参数呢

会带上 effect 的返回值。

All 7 comments

then中是不是有默认返回的参数呢

会带上 effect 的返回值。

这是我调用的地方
image

打印的是response 是后台的数据,已获取到数据,resp 是我打印的.then返回的数据,没有值
image

这是我的effect
image

这是我调用的api
image

按着@sorrycc陈大你说的,应该有返回值的啊 是我哪里用的不对吗?

return response

@sorrycc dispatch触发之后可以在dipatch({}).then中获取全局state的最新状态吗?

我也想知道dispatch触发之后可以在dipatch({}).then中获取全局state的最新状态吗? @LPink777 @Gzbox @amelonsamy @

@LPink777 @ChanseaCem 不行的。dispatch().then 推荐用来做比较 "轻量" 的操作,因为它相当于通知 UI 说 effect 调用结束,则此时 UI 层可以关闭 xx,可以提示 xx。

对于异步逻辑的组织应该尽量在 effect 中完成。

而获取全局 state 的最新状态,是通过 useSelector 获取到的。

你的意思是
effects: {
*getProList({ payload }, { call, put }) {
const res = yield call(getProList);
if (res && res.code === 200) {
yield put({
type: 'setPro',
payload: {
data: res.data
}
})
}
},
}
then的意义表示这个yield call(getProList);的请求结束,而并不是
yield put({
type: 'setPro',
payload: {
data: res.data
}
})
到reducers进行更新赋值结束。如果要拿最新的state就要使用userSelector。
比如:
@connect(({ yieldrate_group, loading }) => ({
...yieldrate_group
}))
class YieldRateGroup extends Component {
constructor(props) {
super(props);
}

componentDidMount() {
this.initData();
}
saveData = (dispatch, data, fn) => {
dispatch({
type: 'yieldrate_group/setState',
payload: {
data: data
}
}).then(() => {
if (fn) {
fn(dispatch);
}
})
}

initData = () => {
const { dispatch } = this.props;
this.getProList(dispatch);
}

// 获取产品列表
getProList = (dispatch) => {
dispatch({
type: 'yieldrate_group/getProList',
payload: {}
}).then(() => {
this.getGrouptList(dispatch)
})
}

//分组
getGrouptList = (dispatch) => {
const { pro_code, start_time, end_time, hours } = this.props;
dispatch({
type: 'yieldrate_group/getYieldAllList',
payload: {
values: {
pro_code: pro_code,
start_time: start_time,
end_time: end_time,
hours: hours,
sort: type == 'ok' ? 'desc' : 'asc',
type: type
}
}
}).then(() => {
......
})
}

......
}

这里的获取产品列表的then后面调用的getGrouptList 方法里,
const { pro_code, start_time, end_time, hours } = this.props 这句代码,不应该直接调用,而是换成userSelector获取。
我这样的理解对吗?
顺便问下,之前我都用这个方式进行获取,都啥问题,就是偶尔会遇见获取最新state的时候是旧数据。我的理解是,这个时候的this.props里面包含的是redecers完结后的数据,所以我怕对then的理解,和this.props这个的理解是不是哪里冲突了或者哪里搞错了 @xc1427

Was this page helpful?
0 / 5 - 0 ratings