by btroncone

Simple syncing between @ngrx store and local storage

453 Stars 90 Forks Last release: Not found MIT License 153 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


Simple syncing between ngrx store and local storage.


depends on @ngrx/store and Angular 2+.


npm install ngrx-store-localstorage --save
  1. Wrap localStorageSync in an exported function.
  2. Include in your meta-reducers array in
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { StoreModule, ActionReducerMap, ActionReducer, MetaReducer } from '@ngrx/store';
import { localStorageSync } from 'ngrx-store-localstorage';
import { reducers } from './reducers';

const reducers: ActionReducerMap = {todos, visibilityFilter};

export function localStorageSyncReducer(reducer: ActionReducer): ActionReducer { return localStorageSync({keys: ['todos']})(reducer); } const metaReducers: Array> = [localStorageSyncReducer];

@NgModule({ imports: [ BrowserModule, StoreModule.forRoot( reducers, {metaReducers} ) ] }) export class MyAppModule {}


localStorageSync(config: LocalStorageConfig): Reducer

Provide state (reducer) keys to sync with local storage. Returns a meta-reducer.


  • config
    An object that matches with the
    is the only required property.


An interface defining the configuration attributes to bootstrap

. The following are properties which compose
: *
(required) State keys to sync with local storage. The keys can be defined in two different formats: *
: 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'] }, ... ]})`). Note: this config cannot go any deeper. So you cannot specify another object inside of the `todos` array for example.

* 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-alone array specified above).

    * `encrypt`: A function that takes a state string and returns an encrypted version of that string.
    e.g. `(state: string) => btoa(state)`

    * `decrypt`: A function that takes a state string and returns a decrypted version of that string.
    e.g. `(state: string) => atob(state)`

  • rehydrate
    : Pull initial state from local storage on startup, this will default to
  • storage
    : Specify an object that conforms to the Web Storage API interface to use, this will default to
  • removeOnUndefined
    : Specify if the state is removed from the storage when the new value is undefined, this will default to
  • storageKeySerializer
    (key: string) => string
    : Custom serialize function for storage keys, used to avoid Storage conflicts.
  • restoreDates
    (boolean? = true): Restore serialized date objects. If you work directly with ISO date strings, set this option to
  • syncCondition
    (state) => boolean
    : When set, sync to storage medium will only occur when this function returns a true boolean. Example:
    (state) => state.config.syncToStorage
    will check the state tree under config.syncToStorage and if true, it will sync to the storage. If undefined or false it will not sync to storage. Often useful for "remember me" options in login.
  • checkStorageAvailability
    (boolean? = false): Specify if the storage availability checking is expected, i.e. for server side rendering / Universal.
  • mergeReducer
    (state: any, rehydratedState: any, action: any) => any
    : Defines the reducer to use to merge the rehydrated state from storage with the state from the ngrx store. If unspecified, defaults to performing a full deepmerge on an
    or an


localStorageSync({keys: ['todos', 'visibilityFilter'], storageKeySerializer: (key) => 'cool_' + key, ... })
. In this example
will use keys
keys to store
slices of state). The key itself is used by default -
(key) => key

Release Notes / Changelog

See for release notes (though it is missing a couple of versions).

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.