Skip to content

Commit

Permalink
[@mantine/hooks] use-local-storage: Fix value not being updated when …
Browse files Browse the repository at this point in the history
…key is changed (#5910)

When key or other value is changed the value is not reloaded
  • Loading branch information
israelins85 committed Mar 26, 2024
1 parent 39d0460 commit ec1adbf
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function createStorage<T>(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<T>(readStorageValue(getInitialValueInEffect));
Expand All @@ -122,13 +122,13 @@ export function createStorage<T>(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) {
Expand All @@ -151,7 +151,7 @@ export function createStorage<T>(type: StorageType, hookName: string) {
useEffect(() => {
const val = readStorageValue();
val !== undefined && setStorageValue(val);
}, []);
}, [readStorageValue, setStorageValue]);

return [value === undefined ? defaultValue : value, setStorageValue, removeStorageValue] as [
T,
Expand Down

0 comments on commit ec1adbf

Please sign in to comment.