Skip to content

Instantly share code, notes, and snippets.

@brunoluiz
Created January 9, 2018 01:11
Show Gist options
  • Save brunoluiz/5b2e6c2318d637d9a4249ab32dda92c6 to your computer and use it in GitHub Desktop.
Save brunoluiz/5b2e6c2318d637d9a4249ab32dda92c6 to your computer and use it in GitHub Desktop.
local-store.js
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