redux-saga 在第一次不起作用



我有一个redux-saga如下:

export function* loadApplianceSaga() {
  try {
    let {request, success, error} = yield take(ActionTypes.APPLIANCE.LOAD);
    request.url = yield select(getResourceLink, request.resource);
    const response = yield call(makeRequest, request);
    if (!response.error) {
      yield put({type: success, payload: response.body});
    } else {
      yield put({type: error, payload: response});
    }
  } catch (e) {
    yield put({type: ActionTypes.REQUEST.CALL_ERROR, error: e});
  }
}
export function* watchLoadAppliance() {
  while (true) {
    yield* takeEvery(ActionTypes.APPLIANCE.LOAD, loadApplianceSaga);
  }
}

和root saga:

export default function* rootSaga() {
  yield [
    fork(watchLoadAppliance)
  ]
}

我第一次遇到loadApplianceSaga不工作的问题。我记录并看到,在第一次它只派遣ActionTypes.APPLIANCE.LOAD行动,然后没有行动被派遣。但在第二次,我可以看到成功的操作或失败的操作被调度。

谁能告诉我出了什么事?提前感谢!

<

更新行动/strong>:

export const loadAppliances = () => {
  return {
    type: ActionTypes.APPLIANCE.LOAD,
    request: {
      resource: Resources.Appliances,
      param: {
        page: 0,
        size: 5,
        sort: 'name,desc'
      },
      header: {
        Accept: 'application/json'
      }
    },
    success: ActionTypes.APPLIANCE.LOAD_SUCCESS,
    error: ActionTypes.APPLIANCE.LOAD_ERROR
  }
};

您使用take两次。试着

export function* loadApplianceSaga(action) {
  try {
    let {request, success, error} = action;
    request.url = yield select(getResourceLink, request.resource);
    const response = yield call(makeRequest, request);
    if (!response.error) {
      yield put({type: success, payload: response.body});
    } else {
      yield put({type: error, payload: response});
    }
  } catch (e) {
    yield put({type: ActionTypes.REQUEST.CALL_ERROR, error: e});
  }
}
export function* watchLoadAppliance() {
  while (true) {
    yield* takeEvery(ActionTypes.APPLIANCE.LOAD, loadApplianceSaga);
  }
}

相关内容

  • 没有找到相关文章

最新更新