在史诗中派遣行动的正确方法是什么?

问题描述 投票:1回答:1

什么是在redux-observable史诗中发送operationReset()的正确方法?

我应该导入实际商店并使用它吗?

It used to be like this, but following store is deprecated, and will be removed

// show operation failed message
(action$, store) => action$.ofType(OPERATION_FAILURE).map(() => (error({
    title: 'Operation Failed',
    message: 'Opps! It didn\'t go through.',
    action: {
        label: 'Try Again',
        autoDismiss: 0,
        callback: () => store.dispatch(operationReset())
    }
}))),
javascript redux redux-observable
1个回答
1
投票

这可能会引发一个更大的问题,即如何使用回调进行通知,因为这意味着您将非JSON可序列化函数作为操作的一部分发送。

我假设你想要仍然匹配反应通知系统。有一种方法可以使用Observable.create来做到这一点:

(action$, store) =>
  action$.pipe(
    ofType(OPERATION_FAILURE),
    mergeMap(() =>
      Observable.create(observer => {
        observer.next(
          error({
            title: "Operation Failed",
            message: "Oops! It didn't go through.",
            action: {
              label: "Try Again",
              autoDismiss: 0,
              callback: () => {
                // Send off a reset action
                observer.next(operationReset());
                // Close off this observable
                observer.complete();
              },
              // If the notification is dismissed separately (can they click an x?)
              onRemove: () => observer.complete()
            }
          })
        );
      })
    )
  );

注意:我仍然不希望将回调作为操作的一部分发送。有趣的是,我的一个项目也使用了该通知系统组件 - 我们有史诗,它将添加通知并根据操作清除它们。所有操作都保持纯粹,并且通知系统是受控制的副作用。

© www.soinside.com 2019 - 2024. All rights reserved.