@reactuses/core 5.0.20 → 5.0.22

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/README.md CHANGED
@@ -86,11 +86,4 @@ This project is heavily inspired by the following awesome projects.
86
86
 
87
87
  If my work has helped you, consider buying me a cup of coffee. Thank you very much🥰!.
88
88
 
89
- [Buy me a coffee](https://www.buymeacoffee.com/lianwenwu)
90
-
91
- ### For Chinese User
92
-
93
- <p float="left">
94
- <img src="https://d21002cb.images-f3o.pages.dev/images/wechat.jpg" alt="Wechat Pay" width="200" />
95
- <img src="https://d21002cb.images-f3o.pages.dev/images/ali.jpg" alt="Ali Pay" width="200" />
96
- </p>
89
+ [Buy me a coffee](https://www.buymeacoffee.com/lianwenwu)
package/dist/index.cjs CHANGED
@@ -618,7 +618,8 @@ function getInitialState$3(key, defaultValue, storage, serializer, onError) {
618
618
  }
619
619
  function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStorage : undefined, options = defaultOptions$1) {
620
620
  let storage;
621
- const { onError = defaultOnError, effectStorageValue } = options;
621
+ const { onError = defaultOnError, effectStorageValue, mountStorageValue, listenToStorageChanges = true } = options;
622
+ const storageValue = mountStorageValue != null ? mountStorageValue : effectStorageValue;
622
623
  try {
623
624
  storage = getStorage();
624
625
  } catch (err) {
@@ -630,7 +631,7 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
630
631
  const [state, setState] = React.useState(getInitialState$3(key, defaultValue, storage, serializer, onError));
631
632
  useDeepCompareEffect(()=>{
632
633
  var _ref;
633
- const data = (_ref = effectStorageValue ? isFunction(effectStorageValue) ? effectStorageValue() : effectStorageValue : defaultValue) != null ? _ref : null;
634
+ const data = (_ref = storageValue ? isFunction(storageValue) ? storageValue() : storageValue : defaultValue) != null ? _ref : null;
634
635
  const getStoredValue = ()=>{
635
636
  try {
636
637
  const raw = storage == null ? void 0 : storage.getItem(key);
@@ -650,7 +651,7 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
650
651
  serializer,
651
652
  storage,
652
653
  onError,
653
- effectStorageValue
654
+ storageValue
654
655
  ]);
655
656
  const updateState = useEvent((valOrFunc)=>{
656
657
  const currentState = isFunction(valOrFunc) ? valOrFunc(state) : valOrFunc;
@@ -665,6 +666,28 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
665
666
  }
666
667
  }
667
668
  });
669
+ const listener = useEvent(()=>{
670
+ try {
671
+ const raw = storage == null ? void 0 : storage.getItem(key);
672
+ if (raw !== undefined && raw !== null) {
673
+ updateState(serializer.read(raw));
674
+ } else {
675
+ updateState(null);
676
+ }
677
+ } catch (e) {
678
+ onError(e);
679
+ }
680
+ });
681
+ React.useEffect(()=>{
682
+ if (listenToStorageChanges) {
683
+ window.addEventListener('storage', listener);
684
+ return ()=>window.removeEventListener('storage', listener);
685
+ }
686
+ return ()=>{};
687
+ }, [
688
+ listenToStorageChanges,
689
+ listener
690
+ ]);
668
691
  return [
669
692
  state,
670
693
  updateState
@@ -677,7 +700,7 @@ const useDarkMode = (options)=>{
677
700
  return window.matchMedia('(prefers-color-scheme: dark)').matches;
678
701
  };
679
702
  const [dark, setDark] = useStorage(storageKey, defaultValue, storage, {
680
- effectStorageValue: value
703
+ mountStorageValue: value
681
704
  });
682
705
  React.useEffect(()=>{
683
706
  var _window;
package/dist/index.d.cts CHANGED
@@ -1448,19 +1448,33 @@ interface Serializer<T> {
1448
1448
  }
1449
1449
  interface UseStorageOptions<T> {
1450
1450
  /**
1451
- * Custom data serialization
1451
+ * @en Custom data serialization
1452
+ * @zh 自定义数据序列化
1452
1453
  */
1453
1454
  serializer?: Serializer<T>;
1454
1455
  /**
1455
- * On error callback
1456
- *
1457
- * Default log error to `console.error`
1456
+ * @en On error callback
1457
+ * @zh 错误回调
1458
+ * @defaultValue `console.error`
1458
1459
  */
1459
1460
  onError?: (error: unknown) => void;
1460
1461
  /**
1461
- * set to storage when nodata in effect, fallback to defaults
1462
+ * @en set to storage when nodata in first mount
1463
+ * @zh 首次挂载时没有数据时设置到 storage
1464
+ * @deprecated
1462
1465
  */
1463
1466
  effectStorageValue?: T | (() => T);
1467
+ /**
1468
+ * @en set to storage when nodata in first mount
1469
+ * @zh 首次挂载时没有数据时设置到 storage
1470
+ */
1471
+ mountStorageValue?: T | (() => T);
1472
+ /**
1473
+ * @en listen to storage changes
1474
+ * @zh 监听 storage 变化
1475
+ * @defaultValue `true`
1476
+ */
1477
+ listenToStorageChanges?: boolean;
1464
1478
  }
1465
1479
 
1466
1480
  declare function useLocalStorage(key: string, defaults: string, options?: UseStorageOptions<string>): readonly [string | null, Dispatch<SetStateAction<string | null>>];
@@ -3117,10 +3131,22 @@ interface UseLocalStorageOptions<T> {
3117
3131
  */
3118
3132
  onError?: (error: unknown) => void;
3119
3133
  /**
3120
- * @en set to storage when storage doesn't has data in effect, fallback to `defaultValue`
3121
- * @zh 当副作用执行的时候没有在 storage 中获取到数据时设置,默认会设置 `defaultValue`
3134
+ * @en set to storage when nodata in first mount, deprecated
3135
+ * @zh 首次挂载时没有数据时设置到 storage, 已弃用
3136
+ * @deprecated
3122
3137
  */
3123
3138
  effectStorageValue?: T | (() => T);
3139
+ /**
3140
+ * @en set to storage when nodata in first mount
3141
+ * @zh 首次挂载时没有数据时设置到 storage
3142
+ */
3143
+ mountStorageValue?: T | (() => T);
3144
+ /**
3145
+ * @en listen to storage changes
3146
+ * @zh 监听 storage 变化
3147
+ * @defaultValue `true`
3148
+ */
3149
+ listenToStorageChanges?: boolean;
3124
3150
  }
3125
3151
  /**
3126
3152
  * @title UseLocalStorageSerializer
@@ -3262,10 +3288,22 @@ interface UseSessionStorageOptions<T> {
3262
3288
  */
3263
3289
  onError?: (error: unknown) => void;
3264
3290
  /**
3265
- * @en set to storage when storage doesn't has data in effect, fallback to `defaultValue`
3266
- * @zh 当副作用执行的时候没有在 storage 中获取到数据时设置,默认会设置 `defaultValue`
3291
+ * @en set to storage when nodata in first mount, deprecated
3292
+ * @zh 首次挂载时没有数据时设置到 storage, 已弃用
3293
+ * @deprecated
3267
3294
  */
3268
3295
  effectStorageValue?: T | (() => T);
3296
+ /**
3297
+ * @en set to storage when nodata in first mount
3298
+ * @zh 首次挂载时没有数据时设置到 storage
3299
+ */
3300
+ mountStorageValue?: T | (() => T);
3301
+ /**
3302
+ * @en listen to storage changes
3303
+ * @zh 监听 storage 变化
3304
+ * @defaultValue `true`
3305
+ */
3306
+ listenToStorageChanges?: boolean;
3269
3307
  }
3270
3308
  /**
3271
3309
  * @title UseSessionStorageSerializer
package/dist/index.d.mts CHANGED
@@ -1448,19 +1448,33 @@ interface Serializer<T> {
1448
1448
  }
1449
1449
  interface UseStorageOptions<T> {
1450
1450
  /**
1451
- * Custom data serialization
1451
+ * @en Custom data serialization
1452
+ * @zh 自定义数据序列化
1452
1453
  */
1453
1454
  serializer?: Serializer<T>;
1454
1455
  /**
1455
- * On error callback
1456
- *
1457
- * Default log error to `console.error`
1456
+ * @en On error callback
1457
+ * @zh 错误回调
1458
+ * @defaultValue `console.error`
1458
1459
  */
1459
1460
  onError?: (error: unknown) => void;
1460
1461
  /**
1461
- * set to storage when nodata in effect, fallback to defaults
1462
+ * @en set to storage when nodata in first mount
1463
+ * @zh 首次挂载时没有数据时设置到 storage
1464
+ * @deprecated
1462
1465
  */
1463
1466
  effectStorageValue?: T | (() => T);
1467
+ /**
1468
+ * @en set to storage when nodata in first mount
1469
+ * @zh 首次挂载时没有数据时设置到 storage
1470
+ */
1471
+ mountStorageValue?: T | (() => T);
1472
+ /**
1473
+ * @en listen to storage changes
1474
+ * @zh 监听 storage 变化
1475
+ * @defaultValue `true`
1476
+ */
1477
+ listenToStorageChanges?: boolean;
1464
1478
  }
1465
1479
 
1466
1480
  declare function useLocalStorage(key: string, defaults: string, options?: UseStorageOptions<string>): readonly [string | null, Dispatch<SetStateAction<string | null>>];
@@ -3117,10 +3131,22 @@ interface UseLocalStorageOptions<T> {
3117
3131
  */
3118
3132
  onError?: (error: unknown) => void;
3119
3133
  /**
3120
- * @en set to storage when storage doesn't has data in effect, fallback to `defaultValue`
3121
- * @zh 当副作用执行的时候没有在 storage 中获取到数据时设置,默认会设置 `defaultValue`
3134
+ * @en set to storage when nodata in first mount, deprecated
3135
+ * @zh 首次挂载时没有数据时设置到 storage, 已弃用
3136
+ * @deprecated
3122
3137
  */
3123
3138
  effectStorageValue?: T | (() => T);
3139
+ /**
3140
+ * @en set to storage when nodata in first mount
3141
+ * @zh 首次挂载时没有数据时设置到 storage
3142
+ */
3143
+ mountStorageValue?: T | (() => T);
3144
+ /**
3145
+ * @en listen to storage changes
3146
+ * @zh 监听 storage 变化
3147
+ * @defaultValue `true`
3148
+ */
3149
+ listenToStorageChanges?: boolean;
3124
3150
  }
3125
3151
  /**
3126
3152
  * @title UseLocalStorageSerializer
@@ -3262,10 +3288,22 @@ interface UseSessionStorageOptions<T> {
3262
3288
  */
3263
3289
  onError?: (error: unknown) => void;
3264
3290
  /**
3265
- * @en set to storage when storage doesn't has data in effect, fallback to `defaultValue`
3266
- * @zh 当副作用执行的时候没有在 storage 中获取到数据时设置,默认会设置 `defaultValue`
3291
+ * @en set to storage when nodata in first mount, deprecated
3292
+ * @zh 首次挂载时没有数据时设置到 storage, 已弃用
3293
+ * @deprecated
3267
3294
  */
3268
3295
  effectStorageValue?: T | (() => T);
3296
+ /**
3297
+ * @en set to storage when nodata in first mount
3298
+ * @zh 首次挂载时没有数据时设置到 storage
3299
+ */
3300
+ mountStorageValue?: T | (() => T);
3301
+ /**
3302
+ * @en listen to storage changes
3303
+ * @zh 监听 storage 变化
3304
+ * @defaultValue `true`
3305
+ */
3306
+ listenToStorageChanges?: boolean;
3269
3307
  }
3270
3308
  /**
3271
3309
  * @title UseSessionStorageSerializer
package/dist/index.d.ts CHANGED
@@ -1448,19 +1448,33 @@ interface Serializer<T> {
1448
1448
  }
1449
1449
  interface UseStorageOptions<T> {
1450
1450
  /**
1451
- * Custom data serialization
1451
+ * @en Custom data serialization
1452
+ * @zh 自定义数据序列化
1452
1453
  */
1453
1454
  serializer?: Serializer<T>;
1454
1455
  /**
1455
- * On error callback
1456
- *
1457
- * Default log error to `console.error`
1456
+ * @en On error callback
1457
+ * @zh 错误回调
1458
+ * @defaultValue `console.error`
1458
1459
  */
1459
1460
  onError?: (error: unknown) => void;
1460
1461
  /**
1461
- * set to storage when nodata in effect, fallback to defaults
1462
+ * @en set to storage when nodata in first mount
1463
+ * @zh 首次挂载时没有数据时设置到 storage
1464
+ * @deprecated
1462
1465
  */
1463
1466
  effectStorageValue?: T | (() => T);
1467
+ /**
1468
+ * @en set to storage when nodata in first mount
1469
+ * @zh 首次挂载时没有数据时设置到 storage
1470
+ */
1471
+ mountStorageValue?: T | (() => T);
1472
+ /**
1473
+ * @en listen to storage changes
1474
+ * @zh 监听 storage 变化
1475
+ * @defaultValue `true`
1476
+ */
1477
+ listenToStorageChanges?: boolean;
1464
1478
  }
1465
1479
 
1466
1480
  declare function useLocalStorage(key: string, defaults: string, options?: UseStorageOptions<string>): readonly [string | null, Dispatch<SetStateAction<string | null>>];
@@ -3117,10 +3131,22 @@ interface UseLocalStorageOptions<T> {
3117
3131
  */
3118
3132
  onError?: (error: unknown) => void;
3119
3133
  /**
3120
- * @en set to storage when storage doesn't has data in effect, fallback to `defaultValue`
3121
- * @zh 当副作用执行的时候没有在 storage 中获取到数据时设置,默认会设置 `defaultValue`
3134
+ * @en set to storage when nodata in first mount, deprecated
3135
+ * @zh 首次挂载时没有数据时设置到 storage, 已弃用
3136
+ * @deprecated
3122
3137
  */
3123
3138
  effectStorageValue?: T | (() => T);
3139
+ /**
3140
+ * @en set to storage when nodata in first mount
3141
+ * @zh 首次挂载时没有数据时设置到 storage
3142
+ */
3143
+ mountStorageValue?: T | (() => T);
3144
+ /**
3145
+ * @en listen to storage changes
3146
+ * @zh 监听 storage 变化
3147
+ * @defaultValue `true`
3148
+ */
3149
+ listenToStorageChanges?: boolean;
3124
3150
  }
3125
3151
  /**
3126
3152
  * @title UseLocalStorageSerializer
@@ -3262,10 +3288,22 @@ interface UseSessionStorageOptions<T> {
3262
3288
  */
3263
3289
  onError?: (error: unknown) => void;
3264
3290
  /**
3265
- * @en set to storage when storage doesn't has data in effect, fallback to `defaultValue`
3266
- * @zh 当副作用执行的时候没有在 storage 中获取到数据时设置,默认会设置 `defaultValue`
3291
+ * @en set to storage when nodata in first mount, deprecated
3292
+ * @zh 首次挂载时没有数据时设置到 storage, 已弃用
3293
+ * @deprecated
3267
3294
  */
3268
3295
  effectStorageValue?: T | (() => T);
3296
+ /**
3297
+ * @en set to storage when nodata in first mount
3298
+ * @zh 首次挂载时没有数据时设置到 storage
3299
+ */
3300
+ mountStorageValue?: T | (() => T);
3301
+ /**
3302
+ * @en listen to storage changes
3303
+ * @zh 监听 storage 变化
3304
+ * @defaultValue `true`
3305
+ */
3306
+ listenToStorageChanges?: boolean;
3269
3307
  }
3270
3308
  /**
3271
3309
  * @title UseSessionStorageSerializer
package/dist/index.mjs CHANGED
@@ -610,7 +610,8 @@ function getInitialState$3(key, defaultValue, storage, serializer, onError) {
610
610
  }
611
611
  function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStorage : undefined, options = defaultOptions$1) {
612
612
  let storage;
613
- const { onError = defaultOnError, effectStorageValue } = options;
613
+ const { onError = defaultOnError, effectStorageValue, mountStorageValue, listenToStorageChanges = true } = options;
614
+ const storageValue = mountStorageValue != null ? mountStorageValue : effectStorageValue;
614
615
  try {
615
616
  storage = getStorage();
616
617
  } catch (err) {
@@ -622,7 +623,7 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
622
623
  const [state, setState] = useState(getInitialState$3(key, defaultValue, storage, serializer, onError));
623
624
  useDeepCompareEffect(()=>{
624
625
  var _ref;
625
- const data = (_ref = effectStorageValue ? isFunction(effectStorageValue) ? effectStorageValue() : effectStorageValue : defaultValue) != null ? _ref : null;
626
+ const data = (_ref = storageValue ? isFunction(storageValue) ? storageValue() : storageValue : defaultValue) != null ? _ref : null;
626
627
  const getStoredValue = ()=>{
627
628
  try {
628
629
  const raw = storage == null ? void 0 : storage.getItem(key);
@@ -642,7 +643,7 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
642
643
  serializer,
643
644
  storage,
644
645
  onError,
645
- effectStorageValue
646
+ storageValue
646
647
  ]);
647
648
  const updateState = useEvent((valOrFunc)=>{
648
649
  const currentState = isFunction(valOrFunc) ? valOrFunc(state) : valOrFunc;
@@ -657,6 +658,28 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
657
658
  }
658
659
  }
659
660
  });
661
+ const listener = useEvent(()=>{
662
+ try {
663
+ const raw = storage == null ? void 0 : storage.getItem(key);
664
+ if (raw !== undefined && raw !== null) {
665
+ updateState(serializer.read(raw));
666
+ } else {
667
+ updateState(null);
668
+ }
669
+ } catch (e) {
670
+ onError(e);
671
+ }
672
+ });
673
+ useEffect(()=>{
674
+ if (listenToStorageChanges) {
675
+ window.addEventListener('storage', listener);
676
+ return ()=>window.removeEventListener('storage', listener);
677
+ }
678
+ return ()=>{};
679
+ }, [
680
+ listenToStorageChanges,
681
+ listener
682
+ ]);
660
683
  return [
661
684
  state,
662
685
  updateState
@@ -669,7 +692,7 @@ const useDarkMode = (options)=>{
669
692
  return window.matchMedia('(prefers-color-scheme: dark)').matches;
670
693
  };
671
694
  const [dark, setDark] = useStorage(storageKey, defaultValue, storage, {
672
- effectStorageValue: value
695
+ mountStorageValue: value
673
696
  });
674
697
  useEffect(()=>{
675
698
  var _window;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactuses/core",
3
- "version": "5.0.20",
3
+ "version": "5.0.22",
4
4
  "license": "Unlicense",
5
5
  "homepage": "https://www.reactuse.com/",
6
6
  "repository": {