Let's say we an action SyncUserData
, which returns observable that listen to changes from the database. Whenever there's a change, the action dispatches the action new PatchUserData(newData)
.
The SyncUserData
action is dispatched (only once) from ngxsOnInit
of the state.
In a different section (e.g., component), I want to do something whenever the action PatchUserData
is dispatching. Something like this:
this.store.onDispatch(PatchUserData).subscribe();
I was looking a bit in the source, and I didn't find something similar to my example.
An alternative to subscribing to the state directly is to use the NGXS action stream to be notified when a specific action has been dispatched/completed see the documentation for action handlers.
From the official doco:
@Component({ ... })
export class CartComponent {
constructor(private actions$: Actions) {}
ngOnInit() {
this.actions$.pipe(ofActionSuccessful(CartDelete)).subscribe(() => alert('Item deleted'));
}
}