5

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.

  • So it's not really a question but rather something you want to show, since you answered it ? –  Oct 03 '19 at 08:11
  • 1
    @Maryannah exactly, I couldn't find relevant questions on SO, but there were [similar unanswered questions in ngrx repo](https://github.com/ngrx/platform/issues/1897#issuecomment-535833183), so I found a solution and decided to share. I know this is encouraged on SE. – Klaster_1 Нет войне Oct 03 '19 at 08:15
  • Sure, that was just to be sure, and make you state it so other users do not have to scratch their head finding a solution. Good for you then ! –  Oct 03 '19 at 08:19

3 Answers3

6

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.

0

Do you have a default return in your switch statement?

 default:
   return state;
Tecayehuatl
  • 172
  • 1
  • 10
  • How is this related to the question? – Klaster_1 Нет войне Jan 05 '20 at 11:02
  • 1
    In my case the console showed me this because in my reducer I didn't had a default return of the state, have you tried? – Tecayehuatl Jan 11 '20 at 23:06
  • 1
    @Klaster_1 I have to agree with Tecayehuatl because I also forgot the default return in my reducer and got similar error hence I find myself here. I have voted up the answer as it is a legitimate answer to solve errors some of us are having troubles dealing with. This answer just saved my day ;-) – omostan Jun 01 '20 at 15:47
-1

try to select the router slice of state with a simple function

export const selectRouter = (state: State) => state.router;

because createFeatureSelector, hence the name, is used for selecting a slice of state added in a FEATURE module

StoreModule.forFeature('feature1', reducers)

in this case you should use createFeatureSelector

export const selectFeature1 = createFeatureSelector<State, Feature1State>('feature1');
oleg gabureac
  • 803
  • 7
  • 10