21 lines
636 B
TypeScript
21 lines
636 B
TypeScript
![]() |
import { useEffect, useState } from 'react';
|
||
|
|
||
|
export function useLocalStorage<T>(key: string, initialValue: T | (() => T)) {
|
||
|
const [value, setValue] = useState<T>(() => {
|
||
|
const storedValues = localStorage.getItem(key);
|
||
|
if (storedValues != null) return JSON.parse(storedValues);
|
||
|
|
||
|
if (typeof initialValue === 'function') {
|
||
|
return (initialValue as () => T)();
|
||
|
} else {
|
||
|
return initialValue;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
useEffect(() => {
|
||
|
localStorage.setItem(key, JSON.stringify(value));
|
||
|
}, [key, value]);
|
||
|
|
||
|
return [value, setValue] as [typeof value, typeof setValue];
|
||
|
}
|