use-local-storage-state
Advanced tools
Comparing version 19.3.1 to 19.4.0
{ | ||
"name": "use-local-storage-state", | ||
"version": "19.3.1", | ||
"version": "19.4.0", | ||
"description": "React hook that persist data in localStorage", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -188,3 +188,4 @@ # `use-local-storage-state` | ||
- [`use-storage-state`](https://github.com/astoilkov/use-storage-state) — Supports `localStorage`, `sessionStorage`, and any other [`Storage`](https://developer.mozilla.org/en-US/docs/Web/API/Storage) compatible API. | ||
- [`use-session-storage-state`](https://github.com/astoilkov/use-session-storage-state) — A clone of this library but for `sessionStorage`. | ||
- [`local-db-storage`](https://github.com/astoilkov/local-db-storage) — Tiny wrapper around `IndexedDB` that mimics `localStorage` API. |
@@ -5,5 +5,5 @@ import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react'; | ||
export default function useLocalStorageState(key, options) { | ||
const serializer = options === null || options === void 0 ? void 0 : options.serializer; | ||
const [defaultValue] = useState(options === null || options === void 0 ? void 0 : options.defaultValue); | ||
return useLocalStorage(key, defaultValue, options === null || options === void 0 ? void 0 : options.storageSync, serializer === null || serializer === void 0 ? void 0 : serializer.parse, serializer === null || serializer === void 0 ? void 0 : serializer.stringify); | ||
const serializer = options?.serializer; | ||
const [defaultValue] = useState(options?.defaultValue); | ||
return useLocalStorage(key, defaultValue, options?.storageSync, serializer?.parse, serializer?.stringify); | ||
} | ||
@@ -31,4 +31,3 @@ function useLocalStorage(key, defaultValue, storageSync = true, parse = parseJSON, stringify = JSON.stringify) { | ||
() => { | ||
var _a; | ||
const string = (_a = goodTry(() => localStorage.getItem(key))) !== null && _a !== void 0 ? _a : null; | ||
const string = goodTry(() => localStorage.getItem(key)) ?? null; | ||
if (inMemoryData.has(key)) { | ||
@@ -42,3 +41,3 @@ storageItem.current.parsed = inMemoryData.get(key); | ||
} | ||
catch (_b) { | ||
catch { | ||
parsed = defaultValue; | ||
@@ -54,3 +53,3 @@ } | ||
// - https://github.com/astoilkov/use-local-storage-state/issues/33 | ||
if (string === null && defaultValue !== undefined) { | ||
if (defaultValue !== undefined && string === null) { | ||
// reasons for `localStorage` to throw an error: | ||
@@ -85,3 +84,3 @@ // - maximum quota is exceeded | ||
} | ||
catch (_a) { | ||
catch { | ||
inMemoryData.set(key, value); | ||
@@ -91,2 +90,7 @@ } | ||
}, [key, stringify]); | ||
const removeItem = useCallback(() => { | ||
goodTry(() => localStorage.removeItem(key)); | ||
inMemoryData.delete(key); | ||
triggerCallbacks(key); | ||
}, [key]); | ||
// - syncs change across tabs, windows, iframes | ||
@@ -100,3 +104,3 @@ // - the `storage` event is called only in all tabs, windows, iframe's except the one that | ||
const onStorage = (e) => { | ||
if (e.storageArea === goodTry(() => localStorage) && e.key === key) { | ||
if (e.key === key && e.storageArea === goodTry(() => localStorage)) { | ||
triggerCallbacks(key); | ||
@@ -113,9 +117,5 @@ } | ||
isPersistent: value === defaultValue || !inMemoryData.has(key), | ||
removeItem() { | ||
goodTry(() => localStorage.removeItem(key)); | ||
inMemoryData.delete(key); | ||
triggerCallbacks(key); | ||
}, | ||
removeItem, | ||
}, | ||
], [key, setState, value, defaultValue]); | ||
], [key, setState, value, defaultValue, removeItem]); | ||
} | ||
@@ -138,3 +138,3 @@ // notifies all instances using the same `key` to update | ||
} | ||
catch (_a) { } | ||
catch { } | ||
} |
190
17421