【问题标题】:IF in Redux Observable epicIF 在 Redux Observable 史诗中
【发布时间】:2018-11-19 12:22:53
【问题描述】:

我有一个史诗,可以捕获获取状态的每次调度(只是状态中的项目,例如 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 provide 'function (source) { return source.lift(新 MapToOperator(值)); }' 需要流的地方。 您可以提供 Observable、Promise、Array 或 Iterable。 在 subscribeTo (subscribeTo.js:41)

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

【问题讨论】:

    标签: rxjs observable redux-observable


    【解决方案1】:

    您需要从传递给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 和 EMPTY 是从 rxjs 导入的。

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 2019-01-23
      • 2017-04-22
      • 2017-05-06
      • 2020-12-30
      • 2018-09-15
      • 2020-06-03
      • 2017-04-14
      • 2018-01-20
      相关资源
      最近更新 更多