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

2

8

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.

Downstage answered 24/2, 2019 at 15:0 Comment(0)
G
6

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)
Gargantua answered 24/2, 2019 at 19:7 Comment(0)
W
0

Or you can put an assertion StoreModule.forRoot({storeReducer} as ActionReducerMap<IAppState>)

Windhover answered 16/12, 2021 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.