added checks to avoid parsing invalid json
This commit is contained in:
parent
56a1c9c7ad
commit
9c7ad256a4
|
@ -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 = () => {
|
||||
|
|
Loading…
Reference in a new issue