angularngrxngrx-reducersngrx-selectors

Why do using reducerManager gives me incorrect state


I'm using reducerManager to add my reducers inside of a global object.

if for example I write

const reducer = createReducer( initialState, on( doSomething, state => state ) );

then:

reducerManager.addReducer( 'reducer', reducer );

and in my component use select I need to write:

val:Observable<any> = this.store.select( state => state.reducer.val );

My question is why can't I just write:

val:Observable<any> = this.store.select( state => state.val );

is this the meaning of using a reducer manager or am I getting something wrong?

I've added many reducers to my manager and after writing the name as state property to get my final value, I always get the same store state ( btw do these states are shared, what's the purpose of it ?)


Solution

  • The state is a global object. By using reducerManager.addReducer( 'reducer', reducer ); you're adding the reducer to the global object, with reducer as key.

    So your state becomes

    {
      reducer: reducer()
    }
    

    To read from the store you always select from the root object, and thus you need to select the state from the reducer property.