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

use single state and local storage

This commit is contained in:
grafixeyehero 2022-10-21 21:47:54 +03:00
parent f40c565e4a
commit 6341a71fec
15 changed files with 485 additions and 508 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];
}