Skip to content

Instantly share code, notes, and snippets.

@anyley
Created October 31, 2016 17:01
Show Gist options
  • Save anyley/9ca134aee471d1b6ce151f91591a0622 to your computer and use it in GitHub Desktop.
Save anyley/9ca134aee471d1b6ce151f91591a0622 to your computer and use it in GitHub Desktop.
export const loadState = (storage) => {
try {
const serializedState = localStorage.getItem(storage)
if (serializedState === null)
return undefined
return JSON.parse(serializedState)
} catch (error) {
return undefined
}
}
export const saveState = (storage, state) => {
try {
const serializedState = JSON.stringify(state)
localStorage.setItem(storage, serializedState)
} catch (error) {
// pass
}
}
export const clearStorage = () => {
localStorage.clear()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment