简体   繁体   English

redux-saga 第一次不起作用

[英]redux-saga doesn't work at the first time

I have a redux-saga as below:我有一个 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);
  }
}

and root saga:和根传奇:

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

I'm facing a problem that loadApplianceSaga doesn't work at the first time.我遇到了一个问题,即loadApplianceSaga第一次不起作用。 I logged and saw that in the first time it only dispatched ActionTypes.APPLIANCE.LOAD action then no action is dispatched.我登录并看到它第一次只调度ActionTypes.APPLIANCE.LOAD操作然后没有调度任何操作。 But in the second time, I can see success action or failed action which are dispatched.但是在第二次,我可以看到发送的成功操作或失败操作。

Could anyone tell me what was wrong?谁能告诉我出了什么问题? Thanks in advance!提前致谢!

Updated action :更新操作

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

you have used take two times.你已经使用take两次。 try尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM