【问题标题】:Running an action creator in response to an action (without saga)运行一个动作创建器来响应一个动作(没有 saga)
【发布时间】:2019-03-10 09:55:27
【问题描述】:

是否有一个用于 redux 的库,它允许运行一个函数以响应特定的操作类型并调度该函数的返回值(可能是一个操作/thunk/promise)?我知道 saga 允许这样做,但 saga 对于这个基本要求有点全功能。

我知道这对我自己来说是相当微不足道的,但如果其他人已经这样做了,那么就没有必要重新发明轮子了。

【问题讨论】:

    标签: redux


    【解决方案1】:

    我会像这样编写自己的中间件:

    export default ({ dispatch }) => (next) => (action) => {
      if (action.type === 'MY_ACTION') {
        const anotherAction = someOtherFunction();
        dispatch(anotherAction); // can use next() if you want to skip all middleware before this one.
      } else {
        next(action);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-11
      • 2020-06-25
      • 2018-06-12
      • 2017-07-14
      • 1970-01-01
      • 2016-02-02
      • 2014-04-30
      • 2021-08-07
      相关资源
      最近更新 更多