redux-simple-storage
v2.0.0
Published
Simple save and load the redux state
Downloads
7
Maintainers
Readme
redux-simple-storage
Save and load the state data to the local storage and file storage
Installation
npm install --save redux-simple-storage
Development
git clone https://github.com/ichigo4800/redux-simple-storage
cd redux-simple-storage
npm install
npm run build
Usage
Example 1 (Use the local storage)
Example of the most simple method of use. Save all of the state that have registered to reducers to local storage.
import storage from 'redux-simple-storage'
let store = createStore(
reducers,
applyMiddleware(
storage.saveState()
)
);
// state data load from local storage
store = storage.loadState(store);
Example 2 (Use the file storage)
If you use file storage. have to set "storage_type" and "path" for config option.
import storage from 'redux-simple-storage'
// save and load file storage a file ("c:\abc\data.json")
storage.setConfig({
storage_type: 'file',
file_path: 'c:\abc',
file_name: 'data.json',
});
let store = createStore(
reducers,
applyMiddleware(
storage.saveState()
)
);
// state data load from file storage
store = storage.loadState(store);
Example 3 (Use the white list filter)
import storage from 'redux-simple-storage'
const reducers = combineReducers({
stateTitle,
stateDate,
stateUser,
});
// save and load "stateTitle" and "stateDate"
storage.setConfig({
filter_type: 'whitelist',
filter_list: [
'stateTitle',
'stateDate',
],
});
let store = createStore(
reducers,
applyMiddleware(
storage.saveState()
)
);
store = storage.loadState(store);
Example 4 (Use the black list filter)
import storage from 'redux-simple-storage'
const reducers = combineReducers({
stateTitle,
stateDate,
stateUser,
});
// save and load the state except "stateTitle"
storage.setConfig({
filter_type: 'blacklist',
filter_list: [
'stateTitle',
],
});
let store = createStore(
reducers,
applyMiddleware(
storage.saveState()
)
);
store = storage.loadState(store);
Example 5 (Use multi storage)
State saved using both of local storage and file storage in the below example.
import storage from 'redux-simple-storage'
const reducers = combineReducers({
stateTitle,
stateDate,
stateUser,
});
// stateTitle use the local storage
const storageA = {
filter_type: 'whitelist',
filter_list: [
'stateTitle',
],
};
// stateDate use the file storage ("c:\abc\date.json")
const storageB = {
storage_type: 'file',
file_path: 'c:\abc',
file_name: 'date.json',
filter_type: 'whitelist',
filter_list: [
'stateDate',
],
};
// stateUser use different file storage a file with the "storageB" ("c:\abc\user.json")
const storageC = {
storage_type: 'file',
file_path: 'c:\abc',
file_name: 'user.json',
filter_type: 'whitelist',
filter_list: [
'stateUser',
],
};
storage.setConfig(
storageA,
storageB,
storageC
);
let store = createStore(
reducers,
applyMiddleware(
storage.saveState()
)
);
// state data load from local storage and file storage
store = storage.loadState(store);
API
.saveState()
.loadState(createStore())
.setConfig({options})
| options | type | default | description | |:-----------|-----------:|:------------------:|:---------------:| |storage_type| string | "local" | "local" or "file" | | filter_type| string | "blacklist" | "whitelist" or "blacklist" | | filter_list| array | [] | set the state name to filter | | file_path | string | "" | If you use file storage. have to set path value | | file_name | string |"redux-states.json" | Can set the file name for save the file | | local_key | string | "redux-states" | local storage key name |
License
WTFPL