Save and load the Redux state with ease.
- Flexible storage engines
- Flexible state merger functions
- 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-storage
// Import redux and all your reducers as usual;;// 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 argumentconst reducer = storage;// Now it's time to decide which storage engine should be used//// Note: The arguments to `createEngine` are different for every engine!;const engine = ;// 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`!const middleware = storage;// As everything is prepared, we can go ahead and combine all parts as usualconst createStoreWithMiddleware = createStore;const store = ;// 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 :)const load = storage;;// Notice that our load function will return a promise that can also be used// to respond to the restore event.;
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 😄
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:
If you pass an array of action types as second argument to
those will be added to a internal blacklist and won't trigger calls to
;const middleware = ;
If you want to whitelist all actions that are allowed to issue a
just specify them as third argument.
;const middleware = ;
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!
The MIT License (MIT) Copyright (c) 2016- Gunjan Soni <firstname.lastname@example.org> Copyright (c) 2015-2016 Michael Contento <email@example.com> 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.