How to silence "The feature name "router" does not exist in the state..." ngrx/router-store warning

Klaster_1 picture Klaster_1 · Oct 3, 2019 · Viewed 8.4k times · Source

When I add "@ngrx/router-store" to my project, it spams the app console in development mode and unit test results with the following message:

The feature name "router" does not exist in the state, therefore createFeatureSelector cannot access it. Be sure it is imported in a loaded module using StoreModule.forRoot('router', ...) or StoreModule.forFeature('router', ...). If the default state is intended to be undefined, as is the case with router state, this development-only warning message can be ignored.

I tried to set router state property type to

router: null |  RouterReducerState<SerializedRouterStateSnapshot>

and initial state value to

router: null

but it clashes with routerReducer type, which only accepts

RouterReducerState<SerializedRouterStateSnapshot>

How do I disable this warning? I honestly find it rather annoying.

Answer

Klaster_1 picture Klaster_1 · Oct 3, 2019

Here's what helped me: do not use createFeatureSelector to create router feature selector, use createSelector instead.

export const routerState = createSelector((state: State) => state.router, value => value)

The issue happens because createFeatureSelector logs a warning if feature value equals to undefined. The code above is equivalent to original implementation, but without a log.

Update: here's a PR that aims to solve the issue.