@wzyjs/hooks 0.3.7 → 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.
@@ -1,2 +1,3 @@
1
1
  export * from './useElement';
2
2
  export * from './useElementScrollVisible';
3
+ export * from './useCacheValue';
package/dist/dom/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './useElement';
2
2
  export * from './useElementScrollVisible';
3
+ export * from './useCacheValue';
@@ -0,0 +1,3 @@
1
+ export declare const useCacheValue: <T>(key: string | undefined, value: T | undefined, onChange: ((value: T | undefined) => void) | undefined) => {
2
+ updateValue: (newValue: T | undefined) => void;
3
+ };
@@ -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.7",
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": "9d0b2447a288c8feee2010ac5f6ac5e67eaa478b"
44
+ "gitHead": "e65ba2fd0a4471a28a92f69e1786606433f7b3c7"
45
45
  }