Skip to content

Commit

Permalink
Use Object.is in useSyncExternalStore
Browse files Browse the repository at this point in the history
  • Loading branch information
zalishchuk committed Oct 24, 2022
1 parent 9d4b2dc commit 528a776
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 4 additions & 3 deletions compat/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 });
}
});
Expand Down
10 changes: 10 additions & 0 deletions compat/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 528a776

Please sign in to comment.