dva的dispatch回调,effects返回请求参数

// 业务中的请求
const params = {
    /* 参数 */
}
new Promise((resolve) => {
      this.props.dispatch({
         type: 'GiftRequest/changeGiftState',
         payload: {
            resolve,
            data: params,
         }
      })
    }).then((res) => {
        if (res.code === 200) {
          notification.success({
            message: '操作成功',
          });

          /* do something */

        } else {
          notification.error({
            message: res.msg,
          });
       }
})
// 在model中的effects

...
namespace: "GiftRequest",
...
*changeGiftState({ payload, query }, { call, put }){
      const { resolve } = payload;
      const response = yield call(***接口**, payload.data, query);
      !!resolve && resolve(response); // 返回数据
},
...

====================================分割线=======================================

又学到另一种有效的dispatch回调方法....补充...

// 业务中的请求

this.props.dispatch({
  type: 'GiftRequest/addGift',
  payload: params,
  callback: (res) => {
    if (res) {
      console.log(res);// 请求完成后返回的结果
    }
  }
});
// model中的effects

...
name: 'GiftRequest',
...
*addGift({ payload, callback }, { call, put }){
  const response = yield call(giftAdd, payload,);
  if (response.code === 200) {
    if (callback && typeof callback === 'function') {
      callback(response); // 返回结果
    }
  } else {
    notification.error({
      message: response.msg,
    });
  }
},
...

 

你可能感兴趣的:(React,Dva)