0

我有一个动作需要在它自己的存储中传输数据,但也在配置存储中。我需要这个,因为添加的数据不同,而且清除方式也不同。

我想知道在这种情况下使用组合减速器是否会更好?或者对多个商店采取行动是一个可以接受的解决方案?

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;
  }
}
4

1 回答 1

1

使用减速器组合,无论是使用combineReducers()还是一些手动方法。不同的减速器处理相同的动作是很常见的。

在 Redux 中,我们几乎从不建议您使用多个商店

于 2016-02-26T17:15:57.173 回答