IF in Redux Observable 史诗

IF in Redux Observable epic

我有一个史诗可以捕获每次获取状态的调度(只是来自状态的项目,例如 state.process:{ status: fail, success, inWork},而不是像 200、500 等请求状态)。 当状态 == 成功(通过从状态获取状态)时,我需要发送另一个动作,如 SET_STATUS_SUCCESS

const getStatus = (action, state) =>
    action.pipe(
        ofType(GET_STATUS),
        withLatestFrom(state),
        mergeMap(([action, state]) => {
            const { status } = state.api.process; //here is what i need, there is no problem with status.
            if (status === "success") {
              return mapTo(SET_STATUS_SUCCESS) //got nothing and error.
}
        })
    );

现在我收到错误:

Uncaught TypeError: You provided 'function (source) { return source.lift(new MapToOperator(value)); }' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at subscribeTo (subscribeTo.js:41)

我该怎么办?我只尝试了 return setStatusSuccess 操作,但它也不起作用。

您需要 return 从传递给 mergeMap 的函数中观察到一个值。试试这个:

const getStatus = (action, state) =>
  action.pipe(
    ofType(GET_STATUS),
    withLatestFrom(state),
    mergeMap(([action, state]) => {
      const { status } = state.api.process;

      if (status === 'success') {
        return of({ type: SET_STATUS_SUCCESS });
      } else {
        return EMPTY;
      }
    }),
  );

of and EMPTY are imported from rxjs.