Last active
November 20, 2024 20:56
-
-
Save DevMasterX/7b824f3369bd511a57b4209fd27fb9cf to your computer and use it in GitHub Desktop.
MODULE. localStorage - save, load, remove
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
const save = (key, value) => { | |
try { | |
const serializedState = JSON.stringify(value); | |
localStorage.setItem(key, serializedState); | |
} catch (error) { | |
console.error('Set state error: ', error.message); | |
} | |
}; | |
const load = key => { | |
try { | |
const serializedState = localStorage.getItem(key); | |
return serializedState === null ? undefined : JSON.parse(serializedState); | |
} catch (error) { | |
console.error('Get state error: ', error.message); | |
} | |
}; | |
const remove = key => { | |
try { | |
localStorage.removeItem(key); | |
} catch (error) { | |
console.error('Remove state error: ', error.message); | |
} | |
}; | |
export default { | |
save, | |
load, | |
remove, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment