diff --git a/compat/src/index.js b/compat/src/index.js index 6b9988951c..c9686074f4 100644 --- a/compat/src/index.js +++ b/compat/src/index.js @@ -27,6 +27,7 @@ import { Children } from './Children'; import { Suspense, lazy } from './suspense'; import { SuspenseList } from './suspense-list'; import { createPortal } from './portals'; +import { is } from './util'; import { hydrate, render, @@ -149,18 +150,18 @@ export function useSyncExternalStore(subscribe, getSnapshot) { _instance._value = value; _instance._getSnapshot = getSnapshot; - if (_instance._value !== getSnapshot()) { + if (!is(_instance._value, getSnapshot())) { forceUpdate({ _instance }); } }, [subscribe, value, getSnapshot]); useEffect(() => { - if (_instance._value !== _instance._getSnapshot()) { + if (!is(_instance._value, _instance._getSnapshot())) { forceUpdate({ _instance }); } return subscribe(() => { - if (_instance._value !== _instance._getSnapshot()) { + if (!is(_instance._value, _instance._getSnapshot())) { forceUpdate({ _instance }); } }); diff --git a/compat/src/util.js b/compat/src/util.js index fa12b09876..a689c0f4fc 100644 --- a/compat/src/util.js +++ b/compat/src/util.js @@ -26,3 +26,13 @@ export function removeNode(node) { let parentNode = node.parentNode; if (parentNode) parentNode.removeChild(node); } + +/** + * Check if two values are the same value + * @param {*} x + * @param {*} y + * @returns {boolean} + */ +export function is(x, y) { + return (x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y); +}