Skip to content

Instantly share code, notes, and snippets.

@leomartinsdev
Created June 20, 2023 18:30
Show Gist options
  • Save leomartinsdev/52cc856eb5165fb9be6860afb188f309 to your computer and use it in GitHub Desktop.
Save leomartinsdev/52cc856eb5165fb9be6860afb188f309 to your computer and use it in GitHub Desktop.
Generic localStorage function to use in your services folder.
export function saveItem(key, value) {
if (typeof value === 'string') {
localStorage.setItem(key, value);
} else {
localStorage.setItem(key, JSON.stringify(value));
}
}
export function getItem(key) {
const storedValue = localStorage.getItem(key);
return storedValue ? JSON.parse(storedValue) : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment