Search code examples
ngrxngrx-storengrx-selectors

ngrx selector not returning number but returning the entire state object instead


I'm trying to make this feature selector to work. But instead of returning a number the selector returns the entire state object (in my case the CounterState)

Here is a my working example: https://stackblitz.com/edit/angular-ivy-ctypd1?file=src%2Fapp%2Fcounter.selectors.ts

I get the following outout on the page: Current Count: [object Object]

I get the following output in the console.

{counter: 1, loaded: true, loading: false}
{counter: 2, loaded: true, loading: false}
{counter: 3, loaded: true, loading: false}

I was expecting just 1,2,3 What am I missing?

Regards,


Solution

  • This is because you're creating a nested tree structure while registering the reducer.

    StoreModule.forFeature(counterFeatureKey, { counter: counterReducer }),
    
    

    This should be (or the selector needs to be tweaked):

    StoreModule.forFeature(counterFeatureKey, counterReducer ),