5
votes

I want to dispatch an action when ngrx store inits (@ngrx/store/init). I created an Effect for that:

    @Effect()
  reloadConext$ = this.actions$.pipe(
    ofType(INIT),
    switchMap(() => {
      console.log('INIT ACTION');
        //dispatch my action here
        return of([]);
    }

The effect is not being fired when store init action is dispatched. I have effects modules for root registered in app.module :

EffectsModule.forRoot([AppEffects]),

If I remove the ofType action filter the event is fired. Does anyone know what the filter for init action is not working?

Thanks in advance.

1

1 Answers

12
votes

I think you are looking for the ROOT_EFFECTS_INIT or the onInitEffects lifecycle method for non root effects.

ROOT_EFFECTS_INIT from the docs:

@Effect()
init$ = this.actions$.pipe(
  ofType(ROOT_EFFECTS_INIT),
  map(action => ...)
);

onInitEffects from the docs:

class UserEffects implements OnInitEffects {
  ngrxOnInitEffects(): Action {
    return { type: '[UserEffects]: Init' };
  }
}