Skip to content

Instantly share code, notes, and snippets.

@danieljpgo
Last active September 18, 2021 05:26
Show Gist options
  • Select an option

  • Save danieljpgo/c13e7d44454b643e3aafc82b1a3c1b15 to your computer and use it in GitHub Desktop.

Select an option

Save danieljpgo/c13e7d44454b643e3aafc82b1a3c1b15 to your computer and use it in GitHub Desktop.
/**
* Returns the current object from local storage associated with the given key.
* @template T
* @param {String} key The key to set in localStorage for this value
* @returns {T|undefined}
*/
export function getLocalStorageData(key) {
const data = window.localStorage.getItem(key);
try {
return data ? JSON.parse(data) : undefined;
} catch {
return undefined;
}
}
/**
* Save the value to local storage associated with the given key.
* @template T
* @param {String} key The key to set in localStorage for this value
* @param {T} body The key to set in localStorage for this value
*/
export function setLocalStorageData(key, body) {
window.localStorage.setItem(key, JSON.stringify(body));
}
/**
* Remove the value from local storage associated with the given key.
* @param {String} key The key to set in localStorage for this value
*/
export function removeLocalStorageData(key) {
window.localStorage.removeItem(key);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment