Created
March 15, 2016 18:49
-
-
Save andrew8088/6b7ce243934a083ba554 to your computer and use it in GitHub Desktop.
A simple way to save your Redux state in localStorage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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