Sylvain Girard
Sylvain Girard

Reputation: 388

ngrx/store init called multiple times

I know this has been addressed multiple times (big issue in the past but should be fixed with 4.0) and that the ngrx example app also uses lazy loading and yet init isn't called multiple times. Still, this very simple setup (which uses a lot of the components of the example app) causes init to be called twice, once for the AppModule and once for the lazy loaded Module1Module. Module2Module is not lazy loaded and plays nice.

Minimal repro repo over here or stackblitz repro.

Is anyone able to shine some sort of light on this behavior?

EDIT: in the app I'm working on, that triggered this question, I'm noticing that, with every new init, the entire action history seems to be dispatched again. Since the app isn't wired to an API yet, this isn't very noticeable except in the console. If this gets wired to an API, that would mean quiet a few unnecessary calls...

Upvotes: 2

Views: 1164

Answers (1)

Wojtek
Wojtek

Reputation: 45

I know it's been a long time since this was asked, but in case if anyone finds themselves here like I did:

In our case we're using ngxs and the @@Init was caled multiple times because we have had a shared module with

imports: [
        NgxsModule.forRoot([
            MyState,
            MyState2
        ]

That module was then imported in other modules.

After moving the NgxsModul.forRoot(...) declaration to the app.module.ts as specified in the ngxs doc it works properly.

Upvotes: 1

Related Questions