Skip to content

Commit 32dbf10

Browse files
committed
Update useObjectState implementation
1 parent e27386f commit 32dbf10

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ function throttle(cb, ms) {
3838
};
3939
}
4040

41-
const dispatchStorageEvent = (key, newValue) => {
41+
function isPlainObject(value) {
42+
return Object.prototype.toString.call(value) === "[object Object]";
43+
}
44+
45+
function dispatchStorageEvent(key, newValue) {
4246
window.dispatchEvent(new StorageEvent("storage", { key, newValue }));
43-
};
47+
}
4448

4549
export function useBattery() {
4650
const [state, setState] = React.useState({
@@ -910,14 +914,16 @@ export function useObjectState(initialValue) {
910914
setState((s) => {
911915
const newState = arg(s);
912916

913-
return {
914-
...s,
915-
...newState,
916-
};
917+
if (isPlainObject(newState)) {
918+
return {
919+
...s,
920+
...newState,
921+
};
922+
}
917923
});
918924
}
919925

920-
if (typeof arg === "object") {
926+
if (isPlainObject(arg)) {
921927
setState((s) => ({
922928
...s,
923929
...arg,

0 commit comments

Comments
 (0)