行动在多个商店

时间:2016-02-26 15:25:22

标签: reactjs redux reducers redux-framework

我有一个动作需要在自己的商店中流动数据,但也需要在配置商店中流动数据。我需要这个,因为添加的数据是不同的&它也会被不同地清除。

我想知道在这种情况下使用联合减速机会更好吗?或者在多家商店行事是可以接受的解决方案吗?

import { PAGE_CHANGE_TITLE } from 'actions/types/page.types';
import { PROJECT_SELECTED } from 'actions/types/projects.types';

const initialState = {
  pages: {
    last: {},
    current: {},
    last5: [],
  },
  project: localStorage.getItem('project') || {},
};

export function configs(state = initialState, action) {

  switch (action.type) {

  case PAGE_CHANGE_TITLE:

    const last5 = [...state.pages.last5];
    last5.unshift(action.data);
    if (last5.length > 5) {
      last5.pop();
    }

    return {
      ...state,
      pages: {
        last: {
          ...state.pages.current,
        },
        current: {
          ...action.data,
        },
        last5: last5,
      },
    };

  case PROJECT_SELECTED:
    return {
      ...state,
      project: {
        ...action.data,
      },
    };

  default:
    return state;
  }
}

1 个答案:

答案 0 :(得分:1)

使用减速器组合,无论是combineReducers()还是某种手动方法。拥有different reducers handling the same action非常常见。

在Redux中,we almost never suggest you to use multiple stores