store icon indicating copy to clipboard operation
store copied to clipboard

Cannot read property 'getState' of null

Open nzjoel1234 opened this issue 10 years ago • 8 comments

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

error

nzjoel1234 avatar Dec 02 '16 11:12 nzjoel1234

This may be a silly question but are you calling ngRedux.configureStore or ngRedux.provideStore anywhere?

SethDavenport avatar Dec 04 '16 00:12 SethDavenport

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()));

nzjoel1234 avatar Dec 04 '16 00:12 nzjoel1234

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 avatar Dec 04 '16 22:12 nzjoel1234

@nzjoel1234 Could you please explain how you solved this problem.

rrag avatar Mar 23 '17 02:03 rrag

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.

nzjoel1234 avatar Mar 23 '17 03:03 nzjoel1234

thanks for the quick reply, so you simply removed the

 -- Lazy-loads ClientsModule
- ---- Imports StoreModule

rrag avatar Mar 23 '17 03:03 rrag

No, I removed provides: [..., NgRedux] from all modules other than my root module

nzjoel1234 avatar Mar 23 '17 03:03 nzjoel1234

ok, interesting, I never had the provides: [..., NgRedux]

but I got it to work like this.

  1. Create a ReduxModule (or StoreModule)
@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();
  }
}
  1. 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 {
}
  1. Do not import ReduxModule or NgReduxModule in any lazy children

I initially did not even have the ReduxModule all of that was inside the AppModule. Thank you for sharing

rrag avatar Mar 23 '17 03:03 rrag