Search code examples
angularrxjsngxs

How can I listen to multple actions in NGXS?


I'm in the process of converting an application from NgRX to NgXs. In NgRx, I could 'listen' to multiple actions in an effect and react appropriately to them. How can I accomplish this using NgXS?

In NgRx, I may have something like the following in an effect

this.actions$.pipe(
 ofType(fromCustomers.pageLoaded),
 ofType(fromCustomers.pageRefreshed)
)

I see in NgXs that the @Action decorator can accept and array of action types, but I'm confused on how get the value from the multiple action types when delcaring the method.


Solution

  • In NGXS, you can listen to multiple actions via @Action decorator, like the following:

    @Action([fromCustomers.pageLoaded, fromCustomers.pageRefreshed])
    pageLoadedOrRefreshed(
      ctx: StateContext<YOUR_STATE_MODEL>,
      payload: fromCustomers.pageLoaded | fromCustomers.pageRefreshed
    ) {
      // do some stuff here...
    }