Skip to content

Instantly share code, notes, and snippets.

@andrew8088
Created March 15, 2016 18:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrew8088/6b7ce243934a083ba554 to your computer and use it in GitHub Desktop.
Save andrew8088/6b7ce243934a083ba554 to your computer and use it in GitHub Desktop.
A simple way to save your Redux state in localStorage.
let propsToSave = [];
export const setProps = (props) => propsToSave = props;
export const getLocalStore = () => JSON.parse(localStorage.getItem('state'));
export const setLocalStore = (store) => {
var toSave = {};
var state = store.getState();
propsToSave.forEach(p => toSave[p] = state[p]);
localStorage.setItem('state', JSON.stringify(toSave));
};
import { setProps, getLocalStore, setLocalStore } from './localStore'
import * as reducers from './reducers';
setProps(['keys', 'to', 'save', 'go', 'here']);
const reducer = combineReducers(reducers);
const store = createStore(reducer, getLocalStore());
function init () {
setLocalStore(store);
// ...
}
store.subscribe(init);
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment