1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

apply suggestion

This commit is contained in:
grafixeyehero 2022-10-27 00:59:46 +03:00
parent 7805e86f70
commit d7e48d30b6
21 changed files with 172 additions and 140 deletions

View file

@ -0,0 +1,20 @@
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];
}