added checks to avoid parsing invalid json

This commit is contained in:
Abhinav-grd 2021-03-13 04:14:15 +05:30
parent 56a1c9c7ad
commit 9c7ad256a4

View file

@ -13,10 +13,18 @@ export const setData = (key: LS_KEYS, value: object) => {
};
export const getData = (key: LS_KEYS) => {
if (typeof localStorage === 'undefined') {
return null;
try {
if (
typeof localStorage === 'undefined' ||
typeof key === 'undefined' ||
typeof localStorage.getItem(key) === 'undefined'
) {
return null;
}
return JSON.parse(localStorage.getItem(key));
} catch (e) {
console.log('Failed to Parse JSON');
}
return JSON.parse(localStorage.getItem(key));
};
export const clearData = () => {