Why must must ngrx / redux effects return actions? Is using a noop action like elm considered bad practice?
By default, an ngrx/effect dispatches an action.
If you want an effect to be 'fire-and-forget', all you need to do is add {dispatch: false}
as an argument to the @Effects()
decorator.
From the @ngrx/effects docs:
Observables decorated with the
@Effect()
decorator are expected to be a stream of actions to be dispatched. Pass{ dispatch: false }
to the decorator to prevent actions from being dispatched.Usage:
class MyEffects { constructor(private actions$: Actions) { } @Effect({ dispatch: false }) logActions$ = this.actions$ .do(action => { console.log(action); });}
Under the hood, this is achieved with the ignoreElements
operator. (Here is the source-code of ngrx/effects if you are interested).ignoreElements
has a built in noop function that gets called every time the effect runs.
In short, there is no need for an explicit noop-action in ngrx/effects. I wouldn't outright call it 'bad practice' to dispatch a noop-action, but it is certainly not necessary when using ngrx.