Package Exports
- redux-easy-persist
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (redux-easy-persist) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
redux-easy-persist
A simple implementation of persistence for redux with minimal configuration.
Installation
npm install redux-easy-persist
or
yarn add redux-easy-persist
Usage
import { applyMiddleware, createStore, combineReducers } from "redux";
import createPersistor, { persistWrapper, hydrate } from "redux-easy-persist";
import AsyncStorage from '@react-native-community/async-storage';
// create a persistor and pass in storeEngine and persistKey
const persistor = createPersistor({
storeEngine: AsyncStorage,
persistKey: 'rootState',
});
// wrap reducers in persistWrapper and pass it to combineReducers
const rootReducer = combineReducers(persistWrapper({
//...YOUR REDUCERS
}));
// create the store and pass in persistor as middleware
const store = createStore(rootReducer, applyMiddleware(persistor));
// call hydrate and pass in store to HYDRATE STORE from Storage
hydrate(store);
// you're all done
export default store
Persistor Options
Peristor {
persistKey: string, // state key in storage
storeEngine: StoreEngine, // StoreEngine; any type of storage that implements "getItem" and "setItem" eg AsyncStorage
whiteList?: Array<string>, // key name of reducers whose states you want to persist (optional)
blackList?: Array<string>, // key names of reducers whose states you want excluded (optional)
}
NB: storeEngine getItem and setItem should be Promises
Example using Expo SecureStore as Storage
import * as SecureStore from 'expo-secure-store';
// expose SecureStore.getItemAsync and SecureStore.setItemAsync as getItem and setItem respectively
const SecureStoreWrapper = {
getItem: SecureStore.getItemAsync,
setItem: SecureStore.setItemAsync,
};
const persistor = createPersistor({
storeEngine: SecureStoreWrapper,
persistKey: 'rootState',
});
// continue with the rest of the setup as shown in usage above
API
createPersistor(config: Persistor) // creates the persistor middleware
hydrate(store: Store) // dispatches the stores hydrate action
clearPersistedState(store: Store) // resets state and clears it from storage
persistWrapper(reducers: any) // receives a map of reduces and makes them persistable
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.