redux-saga第一次不起作用

时间:2016-09-20 15:23:44

标签: javascript reactjs redux react-redux 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动作时,不会调度任何动作。但在第二次,我可以看到发送的成功动作或失败动作。

有谁能告诉我出了什么问题?提前谢谢!

更新了行动

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
  }
};

1 个答案:

答案 0 :(得分:2)

你曾两次使用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);
  }
}