- Notifications
You must be signed in to change notification settings - Fork18
Persistence layer for redux with flexible backends
License
react-stack/redux-storage
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Save and load theRedux state with ease.
- Flexible storage engines
- indexedDb: based on window.indexedDb
- localStorage: based on window.localStorage
- Or for environments without
PromisesupportlocalStorageFakePromise
- Or for environments without
- reactNativeAsyncStorage: based on
react-native/AsyncStorage - remoteEndpoint: save/load via XHR
- Flexible state merger functions
- simple: merge plain old JS structures (default)
- immutablejs: merge plain old JSandImmutableobjects
- Storage engines can be async
- Load and save actions that can be observed
- Various engine decorators
- Black- and whitelist actions from issuing a save operation
npm install --save redux-storageAnd you need to install at least oneredux-storage-engine, asredux-storage is only the"management core".
import*asstoragefrom'redux-storage'// Import redux and all your reducers as usualimport{createStore,applyMiddleware,combineReducers}from'redux';import*asreducersfrom'./reducers';// We need to wrap the base reducer, as this is the place where the loaded// state will be injected.//// Note: The reducer does nothing special! It just listens for the LOAD// action and merge in the provided state :)// Note: A custom merger function can be passed as second argumentconstreducer=storage.reducer(combineReducers(reducers));// Now it's time to decide which storage engine should be used//// Note: The arguments to `createEngine` are different for every engine!importcreateEnginefrom'redux-storage-engine-localstorage';constengine=createEngine('my-save-key');// And with the engine we can create our middleware function. The middleware// is responsible for calling `engine.save` with the current state afer// every dispatched action.//// Note: You can provide a list of action types as second argument, those// actions will be filtered and WON'T trigger calls to `engine.save`!constmiddleware=storage.createMiddleware(engine);// As everything is prepared, we can go ahead and combine all parts as usualconstcreateStoreWithMiddleware=applyMiddleware(middleware)(createStore);conststore=createStoreWithMiddleware(reducer);// At this stage the whole system is in place and every action will trigger// a save operation.//// BUT (!) an existing old state HAS NOT been restored yet! It's up to you to// decide when this should happen. Most of the times you can/should do this// right after the store object has been created.// To load the previous state we create a loader function with our prepared// engine. The result is a function that can be used on any store object you// have at hand :)constload=storage.createLoader(engine);load(store);// Notice that our load function will return a promise that can also be used// to respond to the restore event.load(store).then((newState)=>console.log('Loaded state:',newState)).catch(()=>console.log('Failed to load previous state'));
They all are published as own packages on npm. But as a convention all enginesshare the keywordredux-storage-engine, decorators can be foundwithredux-storage-decorator and mergers withredux-storage-merger. So it's pretty trivial to find allthe additions toredux-storage you need 😄
redux-storage will trigger actions after every load or save operation fromthe underlying engine.
You can use this, for example, to display a loading screen until the old statehas been restored like this:
import{LOAD,SAVE}from'redux-storage';functionstoreageAwareReducer(state={loaded:false},action){switch(action.type){caseLOAD:return{ ...state,loaded:true};caseSAVE:console.log('Something has changed and written to disk!');default:returnstate;}}
If you pass an array of action types as second argument tocreateMiddleware,those will be added to a internal blacklist and won't trigger calls toengine.save.
import{createMiddleware}from'redux-storage'import{APP_START}from'./constants';constmiddleware=createMiddleware(engine,[APP_START]);
If you want to whitelist all actions that are allowed to issue aengine.save,just specify them as third argument.
import{createMiddleware}from'redux-storage'import{SHOULD_SAVE}from'./constants';constmiddleware=createMiddleware(engine,[],[SHOULD_SAVE]);
If you want to skip dispatching a redux action everytime something gets saved,just specify it to the option object, which is the fourth argument.
import{createMiddleware}from'redux-storage'import{SHOULD_SAVE}from'./constants';constmiddleware=createMiddleware(engine,[],[],{disableDispatchSaveAction:true});
A fork ofredux-storage
The original author of the packageredux-storage has decided to deprecate the project and no longer maintained. The package will now be maintained here.
Thank youmichaelcontento for a great library!
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 ofthis software and associated documentation files (the "Software"), to deal inthe Software without restriction, including without limitation the rights touse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies ofthe 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 allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESSFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS ORCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHERIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR INCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.About
Persistence layer for redux with flexible backends
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- JavaScript97.7%
- Makefile2.3%