@wzyjs/hooks 0.3.8 → 0.3.9
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/dom/index.d.ts +1 -0
- package/dist/dom/index.js +1 -0
- package/dist/dom/useCacheValue.d.ts +3 -0
- package/dist/dom/useCacheValue.js +37 -0
- package/package.json +2 -2
package/dist/dom/index.d.ts
CHANGED
package/dist/dom/index.js
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
export const useCacheValue = (key, value, onChange) => {
|
|
3
|
+
const loadedRef = useRef(false);
|
|
4
|
+
// Load from cache on mount
|
|
5
|
+
useEffect(() => {
|
|
6
|
+
if (!key || loadedRef.current)
|
|
7
|
+
return;
|
|
8
|
+
loadedRef.current = true;
|
|
9
|
+
if (value)
|
|
10
|
+
return;
|
|
11
|
+
try {
|
|
12
|
+
const cached = localStorage.getItem(key);
|
|
13
|
+
if (cached) {
|
|
14
|
+
const parsed = JSON.parse(cached);
|
|
15
|
+
if (parsed) {
|
|
16
|
+
onChange?.(parsed);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
console.error('Failed to load from cache', e);
|
|
22
|
+
}
|
|
23
|
+
}, [key]);
|
|
24
|
+
// Save to cache when value changes
|
|
25
|
+
const updateValue = (newValue) => {
|
|
26
|
+
if (key) {
|
|
27
|
+
if (newValue) {
|
|
28
|
+
localStorage.setItem(key, JSON.stringify(newValue));
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
localStorage.removeItem(key);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
onChange?.(newValue);
|
|
35
|
+
};
|
|
36
|
+
return { updateValue };
|
|
37
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wzyjs/hooks",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.9",
|
|
4
4
|
"description": "description",
|
|
5
5
|
"author": "wzy",
|
|
6
6
|
"type": "module",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"react": "^19.1.0",
|
|
42
42
|
"react-use": "^17.4.0"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "e65ba2fd0a4471a28a92f69e1786606433f7b3c7"
|
|
45
45
|
}
|