Skip to content

Instantly share code, notes, and snippets.

@EQuimper
Last active November 25, 2016 19:49
Show Gist options
  • Save EQuimper/39c0f0eca6476bc7c7b6450991605ec6 to your computer and use it in GitHub Desktop.
Save EQuimper/39c0f0eca6476bc7c7b6450991605ec6 to your computer and use it in GitHub Desktop.
LocalStorage persist on redux
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);
}
}
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