在Redux-saga中使用Axios上传文件时更新Redux状态的进度

时间:2019-06-24 01:32:59

标签: reactjs redux axios redux-saga

我正在尝试更新进度。

MySaga.js

...

function* putVideo({payload}){
  const response = yield axios.put(
    payload.signedUrl, payload.file,
    {onUploadProgress: (progessEvent) => {
      console.log(progressEvent) // I can get progress rate in every seconds.

      yield put({ type: 'PROGRESS', progressEvent }); // It doesn't work.

    }}
  );
  if(response.status === 200){
    yield put({ type: 'SUCCESS', response}); // It does work well.
  }
}

MyStore.js

...

const initialState = {
  response: {},
  uploadRates: 0,
  isUploading: false,
}
export default handleActions({
  ['SUCCESS']: (state, action) => {
    return produce(state, draft=> {
      draft.response = action.payload;
      draft.uploadRates = 0;
      draft.isUploading = false;
    });
  },
  ['PROGRESS']: (state, action) => {
    return produce(state, draft => {
      draft.uploadRates = action.payload;
      draft.isUploading = true;
    });
  }
},initialState);

我错过了什么?或我应该学习

0 个答案:

没有答案