我需要触发相同的函数两次.一次是在某个redux状态改变后,一次是在redux动作完成后.目前我使用两个独立的侦听器,我想知道是否可以将predicateactionCreator合并为一个侦听器.我试过了,但没能让它工作.

侦听器中间件文档供参考:https://redux-toolkit.js.org/api/createListenerMiddleware

目前的实施(工作正常):

export const addTemperatureAlertListener2 = (
  startListening: AppStartListening
) => {
  startListening({
    predicate: (action, currentState, originalState) => {
      return (
        currentState.chart.yLimits.yMax !== originalState.chart.yLimits.yMax ||
        currentState.chart.yLimits.yMin !== originalState.chart.yLimits.yMin ||
        currentState.user.tConfig.alertHigherThreshold !==
          originalState.user.tConfig.alertHigherThreshold
      );
    },
    effect: (action, listenerApi) => {
      temperatureAlertEffect(action, listenerApi);
    },
  });
};

export const addTemperatureAlertListener1 = (
  startListening: AppStartListening
) => {
  startListening({
    actionCreator: createPatchRecord.fulfilled,
    effect: (action, listenerApi) => {
      temperatureAlertEffect(action, listenerApi);
    },
  });
};

我try 过但不起作用的事情:

export const addTemperatureAlertListener = (
  startListening: AppStartListening
) => {
  startListening({
    actionCreator: createPatchRecord.fulfilled,
    predicate: (action, currentState, originalState) => {
      return (
        currentState.chart.yLimits.yMax !== originalState.chart.yLimits.yMax ||
        currentState.chart.yLimits.yMin !== originalState.chart.yLimits.yMin ||
        currentState.user.tConfig.alertHigherThreshold !==
          originalState.user.tConfig.alertHigherThreshold
      );
    },
    effect: (action, listenerApi) => {
      temperatureAlertEffect(action, listenerApi);
    },
  });
};

推荐答案

我想知道是否有可能将"谓词"和"actionCreator"合并到一个侦听器中.

不,您不能合并侦听器.详情请看startListening

You must provide exactly one of the four options for deciding when the listener will run: 100, 101, 102, or 103.

这意味着每个侦听器中间件只能 Select 上面的一个和effect回调.

您可能能够做的是"部分"合并它们,即单个函数实例化两个侦听器.

export const addTemperatureAlertListener = (
  startListening: AppStartListening
) => {
  startListening({
    predicate: (action, currentState, originalState) => {
      return (
        currentState.chart.yLimits.yMax !== originalState.chart.yLimits.yMax ||
        currentState.chart.yLimits.yMin !== originalState.chart.yLimits.yMin ||
        currentState.user.tConfig.alertHigherThreshold !==
          originalState.user.tConfig.alertHigherThreshold
      );
    },
    effect: temperatureAlertEffect,
  });

  startListening({
    actionCreator: createPatchRecord.fulfilled,
    effect: temperatureAlertEffect,
  });
};

Typescript相关问答推荐

无需刷新即可让现场访客逆变

在React中实现具有独立页面的嵌套路径的最佳实践是什么?

即使子网是公共的,AWS CDK EventBridge ECS任务也无法分配公共IP地址

处理API响应中的日期对象

如何使类型只能有来自另一个类型的键,但键可以有一个新值,新键应该抛出一个错误

如何用不同的键值初始化角react 形式

当我点击外部按钮时,如何打开Html Select 选项菜单?

为什么从数组中删除一个值会删除打字错误?

如何键入函数以只接受映射到其他一些特定类型的参数类型?

react 路由Use RouteLoaderData类型脚本错误

如何从扩展接口推断泛型?

TypeScrip中的类型安全包装类

正确使用相交类型的打字集

如何缩小函数的返回类型?

有没有办法取消分发元组的联合?

带投掷的收窄类型

接受字符串或数字的排序函数

如何为特定参数定义子路由?

如何在Reaction 18、Reaction-Rout6中的导航栏中获取路由参数

如何在Nextjs路由处理程序中指定响应正文的类型