I am implementing ngrx state management in an Angular 4 app. It was going well until I tried to "hydrate" the app state with the state previously saved in the browser local storage.
I have a question about the Initial State and Ahead of Time Compilation section of ngrx/store
documentation. Specifically, what does the following line mean and how would I go about setting ("dynamically injecting at run-time") initialStateFromSomewhere
to a state retrieved from browser local storage?
/// Pretend this is dynamically injected at runtime
const initialStateFromSomewhere = { counter: 3 };
While creating your reducer, you provide the initial state for the store.
Assume you have a FeatureState
interface FeatureState {
counter: number;
}
Now in your reducer you need to create initialState
const initialState: FeatureState = {
count : 0;
}
This initial State will be supplied to the state in the reducer
export function reducer(state: FeatureState = initialState, action: Action): State {
Now, if you want to dynamically add the initialState
, you can retrieve the initialState
from the store and pass it in the reducer