@reactuses/core 6.1.4 → 6.1.6
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 +2 -5
- package/dist/index.cjs +13 -13
- package/dist/index.mjs +13 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<a href="https://github.com/childrentime/reactuse
|
|
3
|
-
<img src="https://reactuse.com/img/og.png
|
|
4
|
-
</a>
|
|
5
|
-
<a href="https://github.com/childrentime/reactuse#gh-dark-mode-only">
|
|
6
|
-
<img src="https://reactuse.com/img/og-dark.png#gh-dark-mode-only" alt="ReactUse - Collection of essential React Hooks" width="300">
|
|
2
|
+
<a href="https://github.com/childrentime/reactuse">
|
|
3
|
+
<img src="https://reactuse.com/img/og.png" alt="ReactUse - Collection of essential React Hooks" width="300">
|
|
7
4
|
</a>
|
|
8
5
|
</p>
|
|
9
6
|
|
package/dist/index.cjs
CHANGED
|
@@ -638,17 +638,20 @@ function getInitialState$3(key, defaultValue, storage, serializer, onError) {
|
|
|
638
638
|
function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStorage : undefined, options = defaultOptions$1) {
|
|
639
639
|
let storage;
|
|
640
640
|
const { onError = defaultOnError, effectStorageValue, mountStorageValue, listenToStorageChanges = true } = options;
|
|
641
|
-
const
|
|
641
|
+
const storageValueRef = useLatest(mountStorageValue != null ? mountStorageValue : effectStorageValue);
|
|
642
|
+
const onErrorRef = useLatest(onError);
|
|
642
643
|
try {
|
|
643
644
|
storage = getStorage();
|
|
644
645
|
} catch (err) {
|
|
645
|
-
|
|
646
|
+
onErrorRef.current(err);
|
|
646
647
|
}
|
|
647
648
|
const type = guessSerializerType(defaultValue);
|
|
648
649
|
var _options_serializer;
|
|
649
|
-
const
|
|
650
|
-
const [state, setState] = React.useState(getInitialState$3(key, defaultValue, storage,
|
|
650
|
+
const serializerRef = useLatest((_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type]);
|
|
651
|
+
const [state, setState] = React.useState(getInitialState$3(key, defaultValue, storage, serializerRef.current, onError));
|
|
651
652
|
useDeepCompareEffect(()=>{
|
|
653
|
+
const serializer = serializerRef.current;
|
|
654
|
+
const storageValue = storageValueRef.current;
|
|
652
655
|
var _ref;
|
|
653
656
|
const data = (_ref = storageValue ? isFunction(storageValue) ? storageValue() : storageValue : defaultValue) != null ? _ref : null;
|
|
654
657
|
const getStoredValue = ()=>{
|
|
@@ -661,16 +664,13 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
|
|
|
661
664
|
return data;
|
|
662
665
|
}
|
|
663
666
|
} catch (e) {
|
|
664
|
-
|
|
667
|
+
onErrorRef.current(e);
|
|
665
668
|
}
|
|
666
669
|
};
|
|
667
670
|
setState(getStoredValue());
|
|
668
671
|
}, [
|
|
669
672
|
key,
|
|
670
|
-
|
|
671
|
-
storage,
|
|
672
|
-
onError,
|
|
673
|
-
storageValue
|
|
673
|
+
storage
|
|
674
674
|
]);
|
|
675
675
|
const updateState = useEvent((valOrFunc)=>{
|
|
676
676
|
const currentState = isFunction(valOrFunc) ? valOrFunc(state) : valOrFunc;
|
|
@@ -679,9 +679,9 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
|
|
|
679
679
|
storage == null ? void 0 : storage.removeItem(key);
|
|
680
680
|
} else {
|
|
681
681
|
try {
|
|
682
|
-
storage == null ? void 0 : storage.setItem(key,
|
|
682
|
+
storage == null ? void 0 : storage.setItem(key, serializerRef.current.write(currentState));
|
|
683
683
|
} catch (e) {
|
|
684
|
-
|
|
684
|
+
onErrorRef.current(e);
|
|
685
685
|
}
|
|
686
686
|
}
|
|
687
687
|
});
|
|
@@ -689,12 +689,12 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
|
|
|
689
689
|
try {
|
|
690
690
|
const raw = storage == null ? void 0 : storage.getItem(key);
|
|
691
691
|
if (raw !== undefined && raw !== null) {
|
|
692
|
-
updateState(
|
|
692
|
+
updateState(serializerRef.current.read(raw));
|
|
693
693
|
} else {
|
|
694
694
|
updateState(null);
|
|
695
695
|
}
|
|
696
696
|
} catch (e) {
|
|
697
|
-
|
|
697
|
+
onErrorRef.current(e);
|
|
698
698
|
}
|
|
699
699
|
});
|
|
700
700
|
React.useEffect(()=>{
|
package/dist/index.mjs
CHANGED
|
@@ -630,17 +630,20 @@ function getInitialState$3(key, defaultValue, storage, serializer, onError) {
|
|
|
630
630
|
function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStorage : undefined, options = defaultOptions$1) {
|
|
631
631
|
let storage;
|
|
632
632
|
const { onError = defaultOnError, effectStorageValue, mountStorageValue, listenToStorageChanges = true } = options;
|
|
633
|
-
const
|
|
633
|
+
const storageValueRef = useLatest(mountStorageValue != null ? mountStorageValue : effectStorageValue);
|
|
634
|
+
const onErrorRef = useLatest(onError);
|
|
634
635
|
try {
|
|
635
636
|
storage = getStorage();
|
|
636
637
|
} catch (err) {
|
|
637
|
-
|
|
638
|
+
onErrorRef.current(err);
|
|
638
639
|
}
|
|
639
640
|
const type = guessSerializerType(defaultValue);
|
|
640
641
|
var _options_serializer;
|
|
641
|
-
const
|
|
642
|
-
const [state, setState] = useState(getInitialState$3(key, defaultValue, storage,
|
|
642
|
+
const serializerRef = useLatest((_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type]);
|
|
643
|
+
const [state, setState] = useState(getInitialState$3(key, defaultValue, storage, serializerRef.current, onError));
|
|
643
644
|
useDeepCompareEffect(()=>{
|
|
645
|
+
const serializer = serializerRef.current;
|
|
646
|
+
const storageValue = storageValueRef.current;
|
|
644
647
|
var _ref;
|
|
645
648
|
const data = (_ref = storageValue ? isFunction(storageValue) ? storageValue() : storageValue : defaultValue) != null ? _ref : null;
|
|
646
649
|
const getStoredValue = ()=>{
|
|
@@ -653,16 +656,13 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
|
|
|
653
656
|
return data;
|
|
654
657
|
}
|
|
655
658
|
} catch (e) {
|
|
656
|
-
|
|
659
|
+
onErrorRef.current(e);
|
|
657
660
|
}
|
|
658
661
|
};
|
|
659
662
|
setState(getStoredValue());
|
|
660
663
|
}, [
|
|
661
664
|
key,
|
|
662
|
-
|
|
663
|
-
storage,
|
|
664
|
-
onError,
|
|
665
|
-
storageValue
|
|
665
|
+
storage
|
|
666
666
|
]);
|
|
667
667
|
const updateState = useEvent((valOrFunc)=>{
|
|
668
668
|
const currentState = isFunction(valOrFunc) ? valOrFunc(state) : valOrFunc;
|
|
@@ -671,9 +671,9 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
|
|
|
671
671
|
storage == null ? void 0 : storage.removeItem(key);
|
|
672
672
|
} else {
|
|
673
673
|
try {
|
|
674
|
-
storage == null ? void 0 : storage.setItem(key,
|
|
674
|
+
storage == null ? void 0 : storage.setItem(key, serializerRef.current.write(currentState));
|
|
675
675
|
} catch (e) {
|
|
676
|
-
|
|
676
|
+
onErrorRef.current(e);
|
|
677
677
|
}
|
|
678
678
|
}
|
|
679
679
|
});
|
|
@@ -681,12 +681,12 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
|
|
|
681
681
|
try {
|
|
682
682
|
const raw = storage == null ? void 0 : storage.getItem(key);
|
|
683
683
|
if (raw !== undefined && raw !== null) {
|
|
684
|
-
updateState(
|
|
684
|
+
updateState(serializerRef.current.read(raw));
|
|
685
685
|
} else {
|
|
686
686
|
updateState(null);
|
|
687
687
|
}
|
|
688
688
|
} catch (e) {
|
|
689
|
-
|
|
689
|
+
onErrorRef.current(e);
|
|
690
690
|
}
|
|
691
691
|
});
|
|
692
692
|
useEffect(()=>{
|