SEY_91
SEY_91

Reputation: 1687

ngrx/store init store is not working properly

My store contains the following reducers :

export const centralStampState = {
  layoutState : layoutReducer, //this one is not initialized
  eventTabState : eventTabReducer,
  eventTimelineState: eventTimelineReducer,
  eventWorkflowState : eventWorkflowReducer,
  displayLayerState : displayLayerReducer,
  treeState : TreeReducer
}

The problem is that Angular initilize all the reducers , only one 'layoutState' is initilized after firing the first action(Which cause an access to an undefined Object).

I am following the same implementation for all the reducers and states, and I didn't figure out the cause of this strange behavior.

The following is the state of the Store after the initialization and after the first action enter image description here

enter image description here

My implementation is the same for all the reducers.

export function layoutReducer(state: LayoutState = INITIAL_LAYOUT_STATE, action: Action) : LayoutState{
  switch (action.type){
    case UPDATE_SPLIT_CONFIG_DIMENSION :
      return updateSplitConfigDimension(state, action);
    case DISABLE_SPLIT_CONFIGURATION:
      return state;
    case UPDATE_SPLIT_AREA_VISIBILITY :
      return state;
    case RESTORE_DEFAULT_CONFIG_LAYOUT :
      return state;
  }
}

export function eventTabReducer(state: EventTabState = EVENT_TAB_INITIAL_STATE, action: CSAction): EventTabState {
  switch (action.type) {
    case LOAD_SUPPORTED_REPORT_FIELDS:
          return state;
    default:
      return state;
  }
}

Upvotes: 2

Views: 3874

Answers (1)

SEY_91
SEY_91

Reputation: 1687

I found it, if it was JAVA instead of Typescript the code will not compile at all !!!!!!!!

I am missing the default statement inside the switch case, When Angular is dealing with the action @ngrx/store/init the layoutReducer will ignore this action and it seems that Angular will return undefined if it doesn't find the default statement.

export function layoutReducer(state: LayoutState = INITIAL_LAYOUT_STATE, action: Action) : LayoutState{
  switch (action.type){
    case UPDATE_SPLIT_CONFIG_DIMENSION :
      return updateSplitConfigDimension(state, action);
    case DISABLE_SPLIT_CONFIGURATION:
      return state;
    case UPDATE_SPLIT_AREA_VISIBILITY :
      return state;
    case RESTORE_DEFAULT_CONFIG_LAYOUT :
      return state;
    default:
      return state;// with this Angular can perform the ngrx init action
  }
}

Upvotes: 9

Related Questions