1.0.2 • Published 4 years ago

redux-localstorage-saver v1.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

redux-localstorage-saver

Redux middleware for automatical save actions to localStorage with restore by action

Install

npm install redux-localstorage-saver

Usage

Init store:

import { createStore, applyMiddleware } from 'redux';
import localStorageMiddleware from 'redux-localstorage-saver';

import rootReducer from './reducer';

const SAVED_ACTIONS = ['FIRST_ACTION2SAVE', 'SECOND_ACTION2SAVE'];

const store = createStore(rootReducer, 
	applyMiddleware(localStorageMiddleware(SAVED_ACTIONS))
);

Or you can use extended config:

const config = {
  items: <array of saved actions>,
  serialize: <serialize function>,
  deserialize: <deserialize function>,
  prefix: <prefix for saved items names>
};

const store = createStore(rootReducer, 
	applyMiddleware(localStorageMiddleware(config))

Restore state

For restore state you need dispatch plain function "restore":

import { restore } from 'redux-localstorage-saver';

store.dispatch(restore());