Cannot read property 'getState' of null
Hey, thanks a lot for the library! I am using version 4.1.0.
I have some code which is not working. Possibly of interest is that this code is in a lazy loaded module.
I have the following select statement:
@select(selectors.settings.getPageSize) pageSize$: Observable<number>;
This works: (i.e. the page size is printed to the console)
constructor(
private route: ActivatedRoute,
private clientsActions: ClientWorkOrdersActions
) {
this.pageSize$.forEach(pagesize => console.log({pagesize}));
}
But when I try to inject an instance of NgRedux it no longer works (No errors but nothing printed to console):
constructor(
private route: ActivatedRoute,
private store: NgRedux<any>,
private clientsActions: ClientWorkOrdersActions
) {
this.pageSize$.forEach(pagesize => console.log({pagesize}));
}
Running the following code gives the error shown in the attachment:
constructor(
private route: ActivatedRoute,
private store: NgRedux<any>,
private clientsActions: ClientWorkOrdersActions
) {
console.log(selectors.settings.getPageSize(this.store.getState()));
}
See the attachment for more error details.
Thanks

This may be a silly question but are you calling ngRedux.configureStore or ngRedux.provideStore anywhere?
I am calling ngRedux.configureStore. Not sure if I am calling provideStore... I don't have access to the code right now sorry.
Something I should have mentioned is that this code works in a normally imported module but not in a lazy loaded module (but @select works fine in the lazy loaded module):
console.log(selectors.settings.getPageSize(this.store.getState()));
OK, so I have figured this out....
I have the following structure:
- AppModule -- Imports StoreModule -- Lazy-loads ClientsModule ---- Imports StoreModule
StoreModule has provides: [..., NgRedux] -> This was the issue!
From the angular docs [https://angular.io/docs/ts/latest/guide/ngmodule.html#!#shared-module]:
Do not specify app-wide singleton providers in a shared module. A lazy loaded module that imports that shared module will make its own copy of the service.
@nzjoel1234 Could you please explain how you solved this problem.
NgRedux is an app-wide singleton provider. I was specifying NgRedux in a shared module which was referenced by a lazy loaded module. The angular docs specify (as I linked to in my last comment) that you should not do this (and why).
I moved the referenced to the NgRedux provider up to my root module (and removed any other references to NgRedux) and it fixed the problem.
thanks for the quick reply, so you simply removed the
-- Lazy-loads ClientsModule
- ---- Imports StoreModule
No, I removed provides: [..., NgRedux] from all modules other than my root module
ok, interesting, I never had the provides: [..., NgRedux]
but I got it to work like this.
- Create a
ReduxModule(orStoreModule)
@NgModule({
imports: [
CommonModule,
NgReduxModule,
NgReduxRouterModule,
],
declarations: [],
providers: [],
})
export class ReduxModule {
constructor(
ngRedux: NgRedux<any>,
devTools: DevToolsExtension,
ngReduxRouter: NgReduxRouter
) {
const rootReducer = combineReducers({
router: routerReducer,
context: contextReducer,
});
ngRedux.configureStore(
rootReducer,
{},
[],
devTools.isEnabled() ? [ devTools.enhancer() ] : []);
ngReduxRouter.initialize();
}
}
- import that in the
AppModule
@NgModule({
declarations: [
AppComponent,
HomeComponent
],
imports: [
RouterModule.forRoot(routes),
BrowserModule,
FormsModule,
HttpModule,
ReduxModule, // <-- import from step 1
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}
- Do not import
ReduxModuleorNgReduxModulein any lazy children
I initially did not even have the ReduxModule all of that was inside the AppModule. Thank you for sharing