diff options
Diffstat (limited to 'packages/web-util/src/hooks')
-rw-r--r-- | packages/web-util/src/hooks/useMemoryStorage.ts | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/packages/web-util/src/hooks/useMemoryStorage.ts b/packages/web-util/src/hooks/useMemoryStorage.ts index d8be24c1e..1dd263797 100644 --- a/packages/web-util/src/hooks/useMemoryStorage.ts +++ b/packages/web-util/src/hooks/useMemoryStorage.ts @@ -27,37 +27,37 @@ const storage: ObservableMap<string, any> = memoryMap<any>(); //with initial value export function useMemoryStorage<Type = string>( - key: StorageKey<Type>, + key: string, defaultValue: Type, ): Required<StorageState<Type>>; //with initial value export function useMemoryStorage<Type = string>( - key: StorageKey<Type>, + key: string, ): StorageState<Type>; // impl export function useMemoryStorage<Type = string>( - key: StorageKey<Type>, + key: string, defaultValue?: Type, ): StorageState<Type> { const [storedValue, setStoredValue] = useState<Type | undefined>( (): Type | undefined => { - const prev = storage.get(key.id); - return prev; + const prev = storage.get(key); + return prev === undefined ? defaultValue : prev; }, ); useEffect(() => { - return storage.onUpdate(key.id, () => { - const newValue = storage.get(key.id); - setStoredValue(newValue); + return storage.onUpdate(key, () => { + const newValue = storage.get(key); + setStoredValue(newValue === undefined ? defaultValue : newValue); }); }, []); const setValue = (value?: Type): void => { if (value === undefined) { - storage.delete(key.id); + storage.delete(key); } else { - storage.set(key.id, value); + storage.set(key, value); } }; |