mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
20 lines
465 B
TypeScript
20 lines
465 B
TypeScript
import { useEffect, useState } from "react";
|
|
import { getLocalStorage, setLocalStorage } from "../utils/local-storage";
|
|
|
|
type Props<T> = {
|
|
name: string;
|
|
defaultValue: T;
|
|
};
|
|
|
|
export const useLocalStorage = <T>({ name, defaultValue }: Props<T>) => {
|
|
const [value, setValue] = useState<T>(() => {
|
|
return getLocalStorage(name, defaultValue);
|
|
});
|
|
|
|
useEffect(() => {
|
|
setLocalStorage(name, value);
|
|
}, [value]);
|
|
|
|
return [value, setValue] as const;
|
|
};
|