I am using NGRX with Angular v4.
An example of a current pattern would be as follows.
- Dispatch BEGIN_AUTH action which does some stuff and updates the store.
- Dispatch COMPLETE_AUTH action.
The effect that "listens" for the COMPLETE_AUTH action needs to access the store to get data from it in order to call an API (which is the side effect of the COMPLETE_AUTH) action.
I note in NGRX the methods to get the latest from the store have been removed (see: https://github.com/ngrx/store/issues/147) and the general advise now is to use .withLatestFrom(...)
within the effect to get the latest store data.
However, this got me thinking as to if getting the latest value from the store is always an anti pattern as the store data is observable and I should always be subscribing to it.
Is this the case?
In my example, I do have subscriptions for various thing from the store. But in this case I need data from the store in order to make my API call.
What is the best way to proceed?
Your question is fairly broad, but the short answer is no. Getting the latest value is NOT an anti pattern and is a common occurrence for RXjs. It also should be noted that it can be a synchronous operation as well. (which is why this API change confused so many people.)
https://github.com/ngrx/store/blob/6a588ad755cc6511368a084b1bac94b260d24ed1/README.md#getstate-getvalue-and-value
The slightly longer answer is that it could be a bad pattern depending on how your statements are composed in the end.