ngrx/component-store trigger effect when state changes

Viewed 720

I have an angular app with @ngrx/component-store.

when the user selects an entry from a list of devices, I store this into component-store

  component.ts:
  onDeviceClicked(device: DeviceTO) {
    this.inspectionStore.setDeviceSelected(device);
  }

  inspectionStore.ts
  readonly setDeviceSelected = (data: DeviceTO) =>  {this.patchState({selectedDevice: data})};

Now, that the user has selected a device, a side effect should be triggered. However I am not sure how I can trigger a side effect when part of the state changed.

Do I need to do it when updating state?

  readonly setDeviceSelected = (data: DeviceTO) =>  {
    this.patchState({selectedDevice: data});
    this.tiggerMySideEffect(data);
  };

Or is there another way I can tell the sideEffect to listen for changes?

2 Answers

I'd keep the state change pure - even though it's actually not (using patchState).

So introduce another effect, combining the call to setDeviceSelected and tiggerMySideEffect. Make setDeviceSelected private, so that the effect has to be used from the outside ('someone else').

Related