29 lines
598 B
TypeScript
29 lines
598 B
TypeScript
|
|
export const useStorage = (key: string) => {
|
||
|
|
const setItem = (value: unknown) => {
|
||
|
|
try {
|
||
|
|
window.localStorage.setItem(key, JSON.stringify(value));
|
||
|
|
} catch (error) {
|
||
|
|
console.log(error);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const getItem = () => {
|
||
|
|
try {
|
||
|
|
const item = window.localStorage.getItem(key);
|
||
|
|
return item ? JSON.parse(item) : "";
|
||
|
|
} catch (error) {
|
||
|
|
console.log(error);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const removeItem = () => {
|
||
|
|
try {
|
||
|
|
window.localStorage.removeItem(key);
|
||
|
|
} catch (error) {
|
||
|
|
console.log(error);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
return { setItem, getItem, removeItem };
|
||
|
|
};
|