Simple syncing between ngrx store and local storage.
ngrx-store-localstorage
depends on @ngrx/store and Angular 2.
npm install ngrx-store-localstorage --save
- Import
compose
andcombineReducers
from@ngrx/store
and@ngrx/core/compose
. - Invoke the
localStorageSync
function aftercombineReducers
, this receives aLocalStorageConfig
object and assigns the propertykeys
the slices of state you would like to keep synced with local storage. - Optionally specify in the
LocalStorageConfig
whether to rehydrate this state from local storage asinitialState
on application bootstrap with therehydrate
property. - Invoke composed function with application reducers as an argument to
StoreModule.provideStore
.
import { NgModule } from '@angular/core';
import { StoreModule, combineReducers } from '@ngrx/store';
import { compose } from '@ngrx/core/compose';
import { localStorageSync } from 'ngrx-store-localstorage';
import { todos, visibilityFilter } from './reducers';
@NgModule({
imports: [
BrowserModule,
StoreModule.provideStore(
compose(
localStorageSync({keys: ['todos']}),
combineReducers
)({todos, visibilityFilter})
)
]
})
export class MyAppModule {}
Provide state (reducer) keys to sync with local storage. Returns a meta-reducer.
config
An object that matches with theLocalStorageConfig
interface,keys
is the only required property.
An interface defining the configuration attributes to bootstrap localStorageSync
. The following are properties which compose LocalStorageConfig
:
-
keys
(required) State keys to sync with local storage. The keys can be defined in two different formats:-
string[]
: Array of strings representing the state (reducer) keys. Full state will be synced (e.g.localStorageSync({keys: ['todos']})
). -
object[]
: Array of objects where for each object the key represents the state key and the value represents custom serialize/deserialize options. This can be one of the following:-
An array of properties which should be synced. This allows for the partial state sync (e.g.
localStorageSync({keys: [{todos: ['name', 'status'] }, ... ]})
). -
A reviver function as specified in the JSON.parse documentation.
-
An object with properties that specify one or more of the following:
-
serialize: A function that takes a state object and returns a plain json object to pass to json.stringify.
-
deserialize: A function that takes that takes the raw JSON from JSON.parse and builds a state object.
-
replacer: A replacer function as specified in the JSON.stringify documentation.
-
space: The space value to pass JSON.stringify.
-
reviver: A reviver function as specified in the JSON.parse documentation.
-
filter: An array of properties which should be synced (same format as the stand-along array specified above).
-
-
-
-
rehydrate
(optional)boolean
: Pull initial state from local storage on startup, this will default tofalse
. -
storage
(optional)Storage
: Specify an object that conforms to the Storage interface to use, this will default tolocalStorage
. -
removeOnUndefined
(optional)boolean
: Specify if the state is removed from the storage when the new value is undefined, this will default tofalse
. -
storageKeySerializer
(optional)(key: string) => string
: Сustom serialize function for storage keys, used to avoid Storage conflicts. Usage:localStorageSync({keys: ['todos', 'visibilityFilter'], storageKeySerializer: (key) => 'cool_' + key, ... })
. In this exampleStorage
will use keyscool_todos
andcool_visibilityFilter
keys to storetodos
andvisibilityFilter
slices of state). The key itself is used by default -(key) => key
.
localStorageSyncAndClean(keys: any[], rehydrate: boolean = false, removeOnUndefined: boolean = false): Reducer
localStorageSyncAndClean(keys: any[], rehydrate: boolean = false, removeOnUndefined: boolean = false): Reducer
This function is deprecated and soon will be removed, please use localStorageSync(LocalStorageConfig).
A shorthand that wraps the functionalities of localStorageSync
and asumes localStorage
as the storage.
keys
State keys to sync with local storage. The keys can be defined in two different formats:-
(string[]): Array of strings representing the state (reducer) keys. Full state will be synced (e.g.
localStorageSync(['todos'])
). -
(object[]): Array of objects where for each object the key represents the state key and the value represents custom serialize/deserialize options. This can be one of the following:
-
An array of properties which should be synced. This allows for the partial state sync (e.g.
localStorageSync([{todos: ['name', 'status'] }, ... ])
). -
A reviver function as specified in the JSON.parse documentation.
-
An object with properties that specify one or more of the following:
-
serialize: A function that takes a state object and returns a plain json object to pass to json.stringify.
-
deserialize: A function that takes that takes the raw JSON from JSON.parse and builds a state object.
-
replacer: A replacer function as specified in the JSON.stringify documentation.
-
space: The space value to pass JSON.stringify.
-
reviver: A reviver function as specified in the JSON.parse documentation.
-
filter: An array of properties which should be synced (same format as the stand-along array specified above).
-
-
-
rehydrateState
(boolean? = false): Pull initial state from local storage on startup.removeOnUndefined
(boolean? = false): Specify if the state is removed from the storage when the new value is undefined.