Created
January 9, 2018 01:11
-
-
Save brunoluiz/5b2e6c2318d637d9a4249ab32dda92c6 to your computer and use it in GitHub Desktop.
local-store.js
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 getState = (key) => { | |
try { | |
const state = window.localStorage.getItem('state') | |
const data = (state) | |
? JSON.parse(state) | |
: undefined | |
if (!data) return undefined | |
return key ? data[key] : state | |
} catch (e) { | |
console.log('Error on localStorage.getItem', e) | |
return undefined | |
} | |
} | |
export const saveState = (state) => { | |
try { | |
window.localStorage.setItem('state', JSON.stringify(state)) | |
} catch (e) { | |
console.log('Error on localStorage.setItem', e) | |
} | |
} | |
export default { | |
get: getState, | |
save: saveState | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment