Skip to content

Instantly share code, notes, and snippets.

@DevMasterX
Last active November 20, 2024 20:56
Show Gist options
  • Save DevMasterX/7b824f3369bd511a57b4209fd27fb9cf to your computer and use it in GitHub Desktop.
Save DevMasterX/7b824f3369bd511a57b4209fd27fb9cf to your computer and use it in GitHub Desktop.
MODULE. localStorage - save, load, remove
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