【问题标题】:redux-observable How to dispatch multiple actions with pipe?redux-observable 如何使用管道调度多个操作?
【发布时间】:2018-08-15 20:46:31
【问题描述】:

我意识到了

const pingEpic = action$ =>
  action$.ofType('PING')
    .delay(1000) // Asynchronously wait 1000ms then continue
    .mapTo({ type: 'PONG' });

意思

dispatch({ type: 'PING' });
dispatch({ type: 'PONG' });

Howeber,我不知道如何用管道调度两个动作。

我的代码在下面

const signUpEpic = (action$: Observable<Action>) => action$.pipe(
  ofType(actions.GET_DEVICE_TOKEN),
  ofType(actions.SIGN_UP),
  exhaustMap(({ payload }) => request({
    url: 'users',
    method: 'post',
    data: {
      user: {
        email: payload.email,
        password: payload.password,
        device_token: payload.device_token,
        sign_up_with: 'email_and_password',
      },
    },
  }).pipe(
    map(data => camelcaseKeysDeep(data)),
    map(({ user, authToken }) => currentUserActions.successLogin({ user, authToken })),
    catchError((errorMessage: string) => Observable.of(actions.failLogin({ errorMessage }))),
  )),
);

你有什么想法吗?

我也试过下面的代码。

ofType(actions.GET_DEVICE_TOKEN),
mapTo(actions.SIGN_UP),

谢谢

【问题讨论】:

  • 抱歉,不清楚您在问什么。也许您可以将您提供的代码简化为只提供基本内容并详细说明问题?

标签: redux observable redux-observable


【解决方案1】:

您可以使用 mergeMap 来调度多个操作

.pipe(
  mergeMap(data => [
    camelcaseKeysDeep(data)),
    currentUserActions.successLogin(data.user, data.authToken))
  ],
  catchError((errorMessage: string) => Observable.of(actions.failLogin({ errorMessage }))),
)),

【讨论】:

    猜你喜欢
    • 2017-04-14
    • 2018-11-10
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2021-06-30
    • 2018-07-16
    • 1970-01-01
    相关资源
    最近更新 更多