kitzo 2.5.3 → 2.5.4
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.d.ts +6 -1
- package/dist/react/hooks/useLocalStorage.js +11 -12
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -161,7 +161,12 @@ export declare function useDebouncedCallback<Args extends unknown[]>(callback: (
|
|
|
161
161
|
|
|
162
162
|
/**
|
|
163
163
|
* A custom hook that manages a stateful value in `localStorage`,
|
|
164
|
-
*
|
|
164
|
+
* with full SSR safety and cross-tab synchronization.
|
|
165
|
+
*
|
|
166
|
+
* The hook uses a two-phase approach: the server and first client render
|
|
167
|
+
* always use `initialValue` (preventing hydration mismatches), and the
|
|
168
|
+
* real localStorage value is synced in after mount.
|
|
169
|
+
*
|
|
165
170
|
* @example
|
|
166
171
|
* const [theme, setTheme] = useLocalStorage<'light' | 'dark'>('ui-theme', 'light');
|
|
167
172
|
* // Supports functional updates:
|
|
@@ -1,34 +1,33 @@
|
|
|
1
|
-
import
|
|
2
|
-
function
|
|
3
|
-
const n =
|
|
4
|
-
if (typeof window > "u")
|
|
5
|
-
return r;
|
|
1
|
+
import n from "react";
|
|
2
|
+
function w(t, r) {
|
|
3
|
+
const [a, s] = n.useState(r), c = n.useCallback(() => {
|
|
6
4
|
try {
|
|
7
5
|
const e = window.localStorage.getItem(t);
|
|
8
6
|
return e ? JSON.parse(e) : r;
|
|
9
7
|
} catch (e) {
|
|
10
|
-
return console.warn(`Error reading localStorage key
|
|
8
|
+
return console.warn(`Error reading localStorage key "${t}":`, e), r;
|
|
11
9
|
}
|
|
12
|
-
}, [t, r]),
|
|
10
|
+
}, [t, r]), d = n.useCallback(
|
|
13
11
|
(e) => {
|
|
14
12
|
try {
|
|
15
13
|
const o = e instanceof Function ? e(a) : e;
|
|
16
14
|
s(o), typeof window < "u" && (window.localStorage.setItem(t, JSON.stringify(o)), window.dispatchEvent(new Event("local-storage-update")));
|
|
17
15
|
} catch (o) {
|
|
18
|
-
console.warn(`Error setting localStorage key
|
|
16
|
+
console.warn(`Error setting localStorage key "${t}":`, o);
|
|
19
17
|
}
|
|
20
18
|
},
|
|
21
19
|
[t, a]
|
|
22
20
|
);
|
|
23
|
-
return
|
|
21
|
+
return n.useEffect(() => {
|
|
22
|
+
s(c());
|
|
24
23
|
const e = (o) => {
|
|
25
|
-
o.key && o.key !== t || s(
|
|
24
|
+
o.key && o.key !== t || s(c());
|
|
26
25
|
};
|
|
27
26
|
return window.addEventListener("storage", e), window.addEventListener("local-storage-update", e), () => {
|
|
28
27
|
window.removeEventListener("storage", e), window.removeEventListener("local-storage-update", e);
|
|
29
28
|
};
|
|
30
|
-
}, [t,
|
|
29
|
+
}, [t, c]), [a, d];
|
|
31
30
|
}
|
|
32
31
|
export {
|
|
33
|
-
|
|
32
|
+
w as useLocalStorage
|
|
34
33
|
};
|