Skip to content

Instantly share code, notes, and snippets.

@lior-amsalem
Created January 6, 2022 09:55
Show Gist options
  • Save lior-amsalem/b1ea8f1948aebfe15e41f4a8abb4a608 to your computer and use it in GitHub Desktop.
Save lior-amsalem/b1ea8f1948aebfe15e41f4a8abb4a608 to your computer and use it in GitHub Desktop.
//localStorage.js
export const loadState = () => {
try {
const serializedState = localStorage.getItem('state');
if (serializedState === null) {
return undefined;
}
return JSON.parse(serializedState);
} catch (err) {
return undefined;
}
};
export const saveState = (state) => {
try {
const serializedState = JSON.stringify(state);
localStorage.setItem('state', serializedState);
} catch (err) {
console.error(err);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment