Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StoreModule.forRoot() - how to return object without additional key

I am wondering how can I return object of the same type as reducer function:

function storeReducer(
  state = INITIAL_APPLICATION_STATE,
  action: Actions
): ApplicationState {
  switch (action.type) {
    case LOAD_USER_THREADS_ACTION:
      return handleLoadUserThreadsAction(state, action);
    default:
      return state;
  }
}

I expect object of type ApplicationState, but with that approach:

StoreModule.forRoot({storeReducer})

I am getting object with key:

storeReducer:{ // object of type Application State}

I am expecting to get object (without additional storeReducer key):

{//object of type Application State}

Tried also StoreModule.forRoot(storeReducer) but then I am getting empty objects and it is not working.

like image 368
Mateusz Avatar asked Feb 24 '19 15:02

Mateusz


1 Answers

The forRoot method on StoreModule expects and ActionReducerMap, not the result of your reducer.

I typically set mine up in a seperate file like this:

export interface IAppState {
    aPieceOfState: IAPieceOfState;
}

export const reducers: ActionReducerMap<IAppState> = {
    aPieceOfState: aPieceOfStateReducer
};

Then import this to app.module.ts and use it like:

StoreModule.forRoot(reducers)
like image 151
ye-olde-dev Avatar answered Nov 13 '22 11:11

ye-olde-dev