From ec1adbfab2e50376f6cbfbde94d17100644b529e Mon Sep 17 00:00:00 2001 From: Israel Lins Albuquerque Date: Tue, 26 Mar 2024 11:12:56 +0100 Subject: [PATCH] [@mantine/hooks] use-local-storage: Fix value not being updated when key is changed (#5910) When key or other value is changed the value is not reloaded --- .../hooks/src/use-local-storage/create-storage.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/@mantine/hooks/src/use-local-storage/create-storage.ts b/packages/@mantine/hooks/src/use-local-storage/create-storage.ts index b0a56deac22..655ce8d5e3d 100644 --- a/packages/@mantine/hooks/src/use-local-storage/create-storage.ts +++ b/packages/@mantine/hooks/src/use-local-storage/create-storage.ts @@ -100,7 +100,7 @@ export function createStorage(type: StorageType, hookName: string) { const storageValue = getItem(key); return storageValue !== null ? deserialize(storageValue) : (defaultValue as T); }, - [key, defaultValue] + [key, deserialize, defaultValue] ); const [value, setValue] = useState(readStorageValue(getInitialValueInEffect)); @@ -122,13 +122,13 @@ export function createStorage(type: StorageType, hookName: string) { setValue(val); } }, - [key] + [key, serialize] ); const removeStorageValue = useCallback(() => { removeItem(key); window.dispatchEvent(new CustomEvent(eventName, { detail: { key, value: defaultValue } })); - }, []); + }, [defaultValue, key]); useWindowEvent('storage', (event) => { if (event.storageArea === window[type] && event.key === key) { @@ -151,7 +151,7 @@ export function createStorage(type: StorageType, hookName: string) { useEffect(() => { const val = readStorageValue(); val !== undefined && setStorageValue(val); - }, []); + }, [readStorageValue, setStorageValue]); return [value === undefined ? defaultValue : value, setStorageValue, removeStorageValue] as [ T,