mirror of
https://github.com/ente-io/ente.git
synced 2025-05-24 12:09:17 +00:00
25 lines
648 B
TypeScript
25 lines
648 B
TypeScript
import { LS_KEYS, getData, setData } from "@ente/shared/storage/localStorage";
|
|
import { Dispatch, SetStateAction, useEffect, useState } from "react";
|
|
|
|
export function useLocalState<T>(
|
|
key: LS_KEYS,
|
|
initialValue?: T,
|
|
): [T, Dispatch<SetStateAction<T>>] {
|
|
const [value, setValue] = useState<T>(initialValue);
|
|
|
|
useEffect(() => {
|
|
const { value } = getData(key) ?? {};
|
|
if (typeof value !== "undefined") {
|
|
setValue(value);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (typeof value !== "undefined") {
|
|
setData(key, { value });
|
|
}
|
|
}, [value]);
|
|
|
|
return [value, setValue];
|
|
}
|