Dva: 在 effects 里用 call 调用的异步操作,参数里有一项是回调,在回调里 put action

Created on 15 Mar 2017  ·  4Comments  ·  Source: dvajs/dva

@sorrycc

比如,参考 https://github.com/github/fetch/issues/89#issuecomment-256610849 这里写了个带进度回调的上传异步函数 futch,然后在 effects 里

yield call(futch, '/', {}, (event) => {
  put({ type: 'progress', payload: event })
})

这样是不行的,或者写成

yield call(futch, '/', {}, function *(event) {
  yield put({ type: 'progress', payload: event })
})

那在 futch 里怎么触发这个 Generator 函数并传入参数?

Most helpful comment

已完美解决

参考了 https://github.com/dvajs/dva/issues/322 提到的 https://stackoverflow.com/questions/40685288/redux-saga-upload-file-progress-event

使用了 axios,在 Component 里直接发起请求并解析请求的 Promise,在 onUploadProgress 回调里 dispatch 上传进度的消息,使用 CancelToken 来取消上传

这个需求在 effects 里确实不好做

All 4 comments

解决了,用第二种写法,传入 Generator 函数,在 futch

xhr.upload.onprogress = (event) => {
  onProgress(event).next()
}

但是仍然有问题,yield put({ type: 'progress', payload: event }) 这个 Action 无法被对应的 reducer 接收到

已完美解决

参考了 https://github.com/dvajs/dva/issues/322 提到的 https://stackoverflow.com/questions/40685288/redux-saga-upload-file-progress-event

使用了 axios,在 Component 里直接发起请求并解析请求的 Promise,在 onUploadProgress 回调里 dispatch 上传进度的消息,使用 CancelToken 来取消上传

这个需求在 effects 里确实不好做

@mdluo 在Component里直接发起请求感觉是跳过了saga的effects……我感觉解决方法不完美啊。

Was this page helpful?
0 / 5 - 0 ratings