Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Persistent storage of your store - reducer
import { Action, ActionReducer, ActionReducerMap, createFeatureSelector, MetaReducer } from '@ngrx/store';
import * as fromTimezone from './timezone.reducer';
import * as fromNotifications from './notification.reducer';
import { localStorageSync } from 'ngrx-store-localstorage';
export const FEATURE_NAME = 'settings';
const STORE_KEYS_TO_PERSIST = ['timezone', 'notifications'];
export interface SettingsState {
timezone: fromTimezone.TimezoneState;
notifications: fromNotifications.NotificationState;
}
export const reducers: ActionReducerMap<SettingsState> = {
timezone: fromTimezone.timezoneReducer,
notifications: fromNotifications.notificationReducer,
};
export const getSettingsState = createFeatureSelector<SettingsState>(FEATURE_NAME);
export function localStorageSyncReducer(reducer: ActionReducer<SettingsState>): ActionReducer<SettingsState> {
return localStorageSync({
keys: STORE_KEYS_TO_PERSIST,
rehydrate: true,
})(reducer);
}
export const metaReducers: Array<MetaReducer<SettingsState, Action>> = [localStorageSyncReducer];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment