Skip to content

Instantly share code, notes, and snippets.

@arbaaz
Created April 30, 2017 10:56
Show Gist options
  • Save arbaaz/f7330cc362af00b3080a3bd8957ca8e7 to your computer and use it in GitHub Desktop.
Save arbaaz/f7330cc362af00b3080a3bd8957ca8e7 to your computer and use it in GitHub Desktop.
Storage middleware
// utils/storage.js
export default function () {
return next => (reducer, initialState) => {
const store = next(reducer, initialState);
store.subscribe(() => {
const state = store.getState();
saveState(state);
});
return store;
};
}
function saveState(state) {
localStorage.setItem('app', state);
}
// app.js
import storageMiddleware from 'utils/storage';
const enhancer = compose(
applyMiddleware(
thunk,
),
storageMiddleware(),
);
@arbaaz
Copy link
Author

arbaaz commented Apr 30, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment