mirror of
https://github.com/IRBorisov/ConceptPortal.git
synced 2025-06-26 13:00:39 +03:00
27 lines
757 B
TypeScript
27 lines
757 B
TypeScript
![]() |
import { useState, useEffect } from 'react';
|
||
|
|
||
|
function getStorageValue<ValueType>(key: string, defaultValue: ValueType) {
|
||
|
const saved = localStorage.getItem(key);
|
||
|
const initial = saved ? JSON.parse(saved!) : undefined;
|
||
|
return initial || defaultValue;
|
||
|
}
|
||
|
|
||
|
const useLocalStorage =
|
||
|
<ValueType>(key: string, defaultValue: ValueType):
|
||
|
[ValueType, React.Dispatch<React.SetStateAction<ValueType>>] => {
|
||
|
const [value, setValue] = useState<ValueType>(() => {
|
||
|
return getStorageValue(key, defaultValue);
|
||
|
});
|
||
|
|
||
|
useEffect(() => {
|
||
|
if(value === undefined) {
|
||
|
localStorage.removeItem(key)
|
||
|
} else {
|
||
|
localStorage.setItem(key, JSON.stringify(value));
|
||
|
}
|
||
|
}, [key, value]);
|
||
|
|
||
|
return [value, setValue];
|
||
|
};
|
||
|
|
||
|
export default useLocalStorage;
|