ngrtx router-store selectors can't see router state

607 Views Asked by At

I have an ngrx/store app with ngrx/router-store installed. In the application I have three features - two that are "eager" (auth and router) and one lazy (admin). admin contains a slice state named user.

Despite being eager, the router selectors don't seem to work. The console.warn message, (otherwise very clear and instructional!) doesn't really solve the problem as the feature is clearly declared/defined in the app.module.ts:

app.module.ts

StoreModule.forRoot(
  {
    [AUTH_FEATURE_KEY]: AuthFeature.reducer,
    router: routerReducer,
  },
  {
    metaReducers: !environment.production ? [] : [],
    runtimeChecks: {
      strictActionImmutability: true,
      strictStateImmutability: true,
    },
  }
),

router.selectors.ts

export const getRouterState = createFeatureSelector<RouterReducerState<RouterStateUrl>>('router');

export const getCurrentRoute = createSelector(
  getRouterState,
  router => {
    console.log('getCurrentRoute', router) // <-- undefined
    return router.state;
  }
);

export const getRouterStateParams = createSelector(
  getCurrentRoute,
  state => state.params
);

Then in one of my lazy loaded modules I am combining getRouterStateParams with entities:

user.selectors.ts

export const getUserFromRouteParam = createSelector(
  selectEntities,
  fromRouter.getRouterStateParams,
  (entities, params) => {
    console.log(entities, params)
    return entities[params['id']]
  }
)

and this is where the error happens.

enter image description here

Am I doing something fundamentally wrong, or is it some kind of a race condition?

Update

What I tried so far:

  • setting StoreModule.forFeature() for each eager reducer separately, and StoreModule.forRoot({})
  • setting initialNavigation: 'enabled' - this only causes the route slice to appear right off the bat (normally, the reducer kicks in only after the first navigation event), but it doesn't rectify undefineds coming from selectors
  • not using CustomSerializer, instead base on the default serializer and factory delivered getSelectors() - all the selectors that I am interested in, for url, params and queryParams - returned undefined too
  • different settings for navigationActionTiming
  • every possible tip mentioned in this twin-similar issue, including the explicit initialState object
0

There are 0 best solutions below