Save and load the Redux state with ease.
Features
- Flexible storage engines
- Flexible state merger functions
- Storage engines can be async
- Load and save actions that can be observed
- SAVE:
{ type: 'REDUX_STORAGE_SAVE', payload: /* state tree */ }
- LOAD:
{ type: 'REDUX_STORAGE_LOAD', payload: /* state tree */ }
- Various engine decorators
- Black- and whitelist actions from issuing a save operation
Installation
npm install --save redux-storage
And you need to install at least one redux-storage-engine, as
redux-storage is only the "management core".
Usage
import * as storage from 'redux-storage'
import { createStore, applyMiddleware, combineReducers } from 'redux';
import * as reducers from './reducers';
const reducer = storage.reducer(combineReducers(reducers));
import createEngine from 'redux-storage-engine-localstorage';
const engine = createEngine('my-save-key');
const middleware = storage.createMiddleware(engine);
const createStoreWithMiddleware = applyMiddleware(middleware)(createStore);
const store = createStoreWithMiddleware(reducer);
const load = storage.createLoader(engine);
load(store);
load(store)
.then((newState) => console.log('Loaded state:', newState))
.catch(() => console.log('Failed to load previous state'));
Details
Engines, Decorators & Mergers
They all are published as own packages on npm. But as a convention all engines
share the keyword redux-storage-engine, decorators can be found
with redux-storage-decorator and mergers with
redux-storage-merger. So it's pretty trivial to find all
the additions to redux-storage you need :smile:
Actions
redux-storage will trigger actions after every load or save operation from
the underlying engine.
You can use this, for example, to display a loading screen until the old state
has been restored like this:
import { LOAD, SAVE } from 'redux-storage';
function storeageAwareReducer(state = { loaded: false }, action) {
switch (action.type) {
case LOAD:
return { ...state, loaded: true };
case SAVE:
console.log('Something has changed and written to disk!');
default:
return state;
}
}
Middleware
If you pass an array of action types as second argument to createMiddleware
,
those will be added to a internal blacklist and won't trigger calls to
engine.save
.
import { createMiddleware } from 'redux-storage'
import { APP_START } from './constants';
const middleware = createMiddleware(engine, [ APP_START ]);
If you want to whitelist all actions that are allowed to issue a engine.save
,
just specify them as third argument.
import { createMiddleware } from 'redux-storage'
import { SHOULD_SAVE } from './constants';
const middleware = createMiddleware(engine, [], [ SHOULD_SAVE ]);
The original author of the package redux-storage has decided to deprecate the project and no longer maintained. The package will now be maintained here.
Thank you michaelcontento for a great library!
License
The MIT License (MIT)
Copyright (c) 2016- Gunjan Soni <gunjan.soni2002@gmail.com>
Copyright (c) 2015-2016 Michael Contento <mail@michaelcontento.de>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.