React-boilerplate,reducer向我发送一个对象而不是默认的initialState吗?

时间:2018-11-28 12:49:06

标签: reactjs react-redux redux-saga saga react-boilerplate

ajax请求挂起时,我没有获得initialState吗? 这是实际数据和减速器初始状态的快照。

this.props of my application

这是我的减速器代码:

/*
 *
 * MainPage reducer
 *
 */
import { fromJS } from 'immutable';
import { LOAD_BANNERS, LOAD_BANNERS_SUCCESS, LOAD_BANNERS_ERROR, LOAD_CAROUSELS, LOAD_CAROUSELS_SUCCESS, LOAD_CAROUSELS_ERROR, LOAD_SIDEBAR, LOAD_SIDEBAR_SUCCESS, LOAD_SIDEBAR_ERROR } from './constants';

export const initialState = fromJS({
  bannerItems: [],
  carouselItems: [],
  sidebarItems: [],
  loadingBanners: false,
  errorBanners: true,
  loadingCarousels: false,
  errorCarousels: true,
  loadingSidebar: false,
  errorSidebar: true
});

function mainPageReducer(state = initialState, action) {
  switch (action.type) {
    case LOAD_BANNERS:
      return state.set('loadingBanners', true).set('errorBanners', false);
    case LOAD_BANNERS_SUCCESS:
      return state.set('loadingBanners', false).set('errorBanners', false).set('bannerItems', action.bannerItems);
    case LOAD_BANNERS_ERROR:
      return state.set('loadingBanners', false).set('errorBanners', action.error);
    case LOAD_CAROUSELS:
      return state.set('loadingCarousels', true).set('errorCarousels', false);
    case LOAD_CAROUSELS_SUCCESS:
      return state.set('loadingCarousels', false).set('errorCarousels', false).set('carouselItems', action.carouselItems);
    case LOAD_CAROUSELS_ERROR:
      return state.set('loadingCarousels', false).set('errorCarousels', action.error);
    case LOAD_SIDEBAR:
      return state.set('loadingSidebars', true).set('errorSidebar', false);
    case LOAD_SIDEBAR_SUCCESS:
      return state.set('loadingSidebars', false).set('errorSidebar', false).set('sidebarItems', action.sidebarItems);
    case LOAD_SIDEBAR_ERROR:
      return state.set('loadingSidebars', false).set('errorSidebar', action.error);
    default:
      return state;
  }
}

export default mainPageReducer;

为什么reducer向我发送一个对象(列表)而不是我的初始状态。 这是我绑定了dataTypes的app.js代码:

MainPage.propTypes = {
  // dispatch: PropTypes.func.isRequired,
  bannerItems: PropTypes.array,
  bannerItemsLoading: PropTypes.bool,
  carouselItems: PropTypes.array,
  carouselItemsLoading: PropTypes.bool,
  sidebarItems: PropTypes.array,
  sidebarItemsLoading: PropTypes.bool,
  loadBanners: PropTypes.func,
  loadCarousels: PropTypes.func,
  loadSidebars: PropTypes.func
};

1 个答案:

答案 0 :(得分:0)

它向您发送了一个不可变js对象,尝试对 ERRORS: groups.Cluster.members: (fields.E339) 'UserGroup.group' is not a foreign key to 'User'. HINT: Did you mean one of the following foreign keys to 'User': user? groups.Cluster.members: (fields.E339) 'UserGroup.user' is not a foreign key to 'Cluster'. HINT: Did you mean one of the following foreign keys to 'Cluster': group? 的项目进行.toJS(),您将获得普通的JavaScript对象。(但是不建议在不可变js之间来回转换和普通的js对象,因为它对性能产生了很大的影响)