Redux 我可以在单独的减速器中使用一种动作类型吗? [英] Redux can I use one action type in separate reducers?

查看:46
本文介绍了Redux 我可以在单独的减速器中使用一种动作类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Redux 应用程序中,我目前有 3 个独立的 reducer,它们处理从 api 获取数据的情况.我的一个减速器的例子是:

I have a situation in my Redux application where I currently have 3 separate reducers that handle the fetching of data from an api. An example of one of my reducers would be:

const INITIAL_STATE = {
  data: [],
  loading: false,
  error: ''
};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case GET_ALL_ORDERS_START:
      return {
        ...state,
        loading: true
      };
    case GET_ALL_ORDERS_SUCCESS:
      return {
        ...state,
        allOrders: action.payload,
        loading: false
      };
    case GET_ALL_ORDERS_FAIL:
      return {
        ...state,
        loading: false,
        error: action.payload
      };
    default:
      return state;
  }
};

注意加载和错误状态,这些在每个当前的 reducer 中都是相同的,并将用于我编写的任何涉及从 api 获取数据的后续 reducer.

Note the loading and error states, these are identical in each current reducer and will be for any subsequent reducers I write that involve fetching data from the api.

我想添加一个仅用于加载和错误状态的reducer.其他 3 个将存储数据.

I would like to add a further reducer that is used only for the loading and error pieces of state. The other 3 would store the data.

这会给我:

数据缩减器 x 3

const INITIAL_STATE = {
  data: []
  // any other state in the future
};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case GET_ALL_ORDERS_SUCCESS:
      return {
        ...state,
        allOrders: action.payload
      };
    default:
      return state;
  }
};

加载/错误减少器(处理整个应用程序的加载/错误)

Loading / Error reducer (handles loading / error for entire app)

const INITIAL_STATE = {
  loading: false,
  error: ''
};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case GET_ALL_ORDERS_START:
      return {
        ...state,
        loading: true
      };
    case GET_ALL_ORDERS_SUCCESS:
      return {
        ...state,
        loading: false
      };
    case GET_ALL_ORDERS_FAIL:
      return {
        ...state,
        loading: false,
        error: action.payload
      };
    default:
      return state;
  }
};

如您所见,这意味着 GET_ALL_ORDER_SUCCESS 操作类型将在 2 个单独的减速器中使用.我的问题是,这样好吗?还是违反惯例?

As you can see this means the GET_ALL_ORDER_SUCCESS action type will be used in 2 separate reducers. My question is, is this ok? or does it go against convention?

非常感谢.

推荐答案

我认为这完全没问题.没有地方说明 Actions 和 Reducers 有 1:1 的映射.实际上,Redux 的创建者明确表示它们之间没有关系,许多reducer 可以对单个action 做出反应,单个reducer 可以对多个action 做出反应.

I think that's perfectly fine. There is no place to states that Actions and Reducers have a 1:1 mapping. In fact, creator of Redux explicitly says that there is no relation between them, many reducers can react to a single actions, a single reducer can react to multiple actions.

我认为他说得最好:https://github.com/reduxible/reduxible/issues/8

推文:https://twitter.com/dan_abramov/status/691608868628119552

相关 SO:Redux:为什么不放置操作和减速器在同一个文件中?

这篇关于Redux 我可以在单独的减速器中使用一种动作类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆