I am working my way through some ngrx tutorials and I think I'm starting to get my brain wrapped around it.
What I don't understand is how to do something as simple as getting a value from the Store
:
Goal: Get a value from the store without having to subscribe
to it.
IE: store.myStoreProperty
or store.getValue(<selector>)
or ?
From what I understand the only way to grab a value from the store is to do something like this:
private readonly _store: Store<ApplicationState>;
// ...
this._store.select(state => state.currentUser).subscribe(user => {
if (!user) { return; }
// ...
});
Question: Is there any possible way to "instantly" get a value from the store without having to subscribe?
I might just be having trouble wrapping my brain around selectors but I kind of thought that was what they were for. Example from the docs:
import { createSelector } from '@ngrx/store';
export interface FeatureState {
counter: number;
}
export interface AppState {
feature: FeatureState;
}
export const selectFeature = (state: AppState) => state.feature;
export const selectFeatureCount = createSelector(
selectFeature,
(state: FeatureState) => state.counter
);
In this example, I was thinking that I could just call selectFeature
or pass it into this._store.select(selectFeature)
to get the actual value, but it returns an observable
(and thus wanting you to subscribe to it).
My main use case here is that I want to access current user info throughout the lifecycle of my app. I have an effect
that is getting that information from the server, and it all works great. However, I am a little confused on how I can simply just access the value from the store
without having to sprinkle .subscribe
everywhere.
And yes, I have seen this but it doesn't help me.