mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
apply suggestion
This commit is contained in:
parent
7805e86f70
commit
d7e48d30b6
21 changed files with 172 additions and 140 deletions
20
src/hooks/useLocalStorage.tsx
Normal file
20
src/hooks/useLocalStorage.tsx
Normal 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];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue