use-json-localstorage 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +6 -3
- package/dist/index.js +7 -4
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -84,18 +84,21 @@ var getRuntimeLocalStorage = () => {
|
|
|
84
84
|
// src/useLocalStorageValue.ts
|
|
85
85
|
var import_react = require("react");
|
|
86
86
|
var useLocalStorageValue = (key, defaultValue) => {
|
|
87
|
-
const store = getRuntimeLocalStorage();
|
|
87
|
+
const store = (0, import_react.useRef)(getRuntimeLocalStorage());
|
|
88
88
|
return (0, import_react.useSyncExternalStore)(
|
|
89
89
|
(onStoreChange) => {
|
|
90
|
-
return store.on("set", (event) => {
|
|
90
|
+
return store.current.on("set", (event) => {
|
|
91
91
|
if (event.key === key) {
|
|
92
92
|
onStoreChange();
|
|
93
93
|
}
|
|
94
94
|
});
|
|
95
95
|
},
|
|
96
96
|
() => {
|
|
97
|
-
const value = store.get(key);
|
|
97
|
+
const value = store.current.get(key);
|
|
98
98
|
return value != null ? value : defaultValue;
|
|
99
|
+
},
|
|
100
|
+
() => {
|
|
101
|
+
return defaultValue;
|
|
99
102
|
}
|
|
100
103
|
);
|
|
101
104
|
};
|
package/dist/index.js
CHANGED
|
@@ -45,20 +45,23 @@ var getRuntimeLocalStorage = () => {
|
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
// src/useLocalStorageValue.ts
|
|
48
|
-
import { useSyncExternalStore } from "react";
|
|
48
|
+
import { useRef, useSyncExternalStore } from "react";
|
|
49
49
|
var useLocalStorageValue = (key, defaultValue) => {
|
|
50
|
-
const store = getRuntimeLocalStorage();
|
|
50
|
+
const store = useRef(getRuntimeLocalStorage());
|
|
51
51
|
return useSyncExternalStore(
|
|
52
52
|
(onStoreChange) => {
|
|
53
|
-
return store.on("set", (event) => {
|
|
53
|
+
return store.current.on("set", (event) => {
|
|
54
54
|
if (event.key === key) {
|
|
55
55
|
onStoreChange();
|
|
56
56
|
}
|
|
57
57
|
});
|
|
58
58
|
},
|
|
59
59
|
() => {
|
|
60
|
-
const value = store.get(key);
|
|
60
|
+
const value = store.current.get(key);
|
|
61
61
|
return value != null ? value : defaultValue;
|
|
62
|
+
},
|
|
63
|
+
() => {
|
|
64
|
+
return defaultValue;
|
|
62
65
|
}
|
|
63
66
|
);
|
|
64
67
|
};
|