Issue
I dispatch my ngrx action with:
store.dispatch(new MyAction(payload));
MyAction has an associated effect. When I dispatch the action the effect fires. Great!
This works as intended. But I only want the effect to fire sometimes. Can you do something like this?
store.dispatch(new MyAction(payload), {triggerEffect: false});
Or do I need to create a second action that is handled exactly the same as MyAction by the reducers and simply not attach an effect to the second Action? I'd rather not go down this route if I can avoid it.
Solution
I don't think there is a built-in solution for this, but you can use a payload property to control the effect execution:
Add the property to your action creator:
class MyAction {
readonly type = MY_ACTION;
constructor(private payload: {data: any, shouldTriggerEffect: boolean}) {}
}
Filter the effect:
@Effect()
myAction$ = this.actions$.pipe(
ofType(ActionTypes.MY_ACTION),
filter(action => action.payload.shouldTriggerSideEffect),
// do your stuff
);
Answered By - Kim Kern
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.