Last active
November 25, 2016 19:49
-
-
Save EQuimper/39c0f0eca6476bc7c7b6450991605ec6 to your computer and use it in GitHub Desktop.
LocalStorage persist on redux
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
export const loadState = () => { | |
try { | |
const auth = localStorage.getItem('name'); | |
if (auth === null) return undefined; | |
return JSON.parse(auth); | |
} catch (err) { | |
return undefined; | |
} | |
} | |
export const saveState = state => { | |
try { | |
const auth = JSON.stringify(state); | |
localStorage.setItem('name', auth); | |
} catch (err) { | |
console.log(err); | |
} | |
} |
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 { loadState, saveState } from '../Data/localStorage'; | |
import throttle from 'lodash/throttle'; | |
const persistedState = loadState(); | |
const store = createStore( | |
rootReducer, | |
persistedState, | |
enhancers | |
); | |
store.subscribe(throttle(() => saveState({ | |
auth: store.getState().auth | |
}), 1000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment