mirror of
https://github.com/Unleash/unleash.git
synced 2025-06-18 01:18:23 +02:00
20 lines
585 B
TypeScript
20 lines
585 B
TypeScript
import { useEffect, useState } from 'react';
|
|
import { createLocalStorage } from '../utils/createLocalStorage';
|
|
|
|
export const useLocalStorageState = <T extends object | string>(
|
|
key: string,
|
|
initialValue: T,
|
|
timeToLive?: number,
|
|
) => {
|
|
const { value: initialStoredValue, setValue: setStoredValue } =
|
|
createLocalStorage<T>(key, initialValue, timeToLive);
|
|
|
|
const [localValue, setLocalValue] = useState<T>(initialStoredValue);
|
|
|
|
useEffect(() => {
|
|
setStoredValue(localValue);
|
|
}, [localValue]);
|
|
|
|
return [localValue, setLocalValue] as const;
|
|
};
|