Skip to content

Commit

Permalink
Try: Check values returned by the 'useSelect' hook for referential st…
Browse files Browse the repository at this point in the history
…ability
  • Loading branch information
Mamaduka committed Aug 15, 2023
1 parent fa552b5 commit 39b92c6
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/data/src/components/use-select/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function Store( registry, suspense ) {
let lastMapResultValid = false;
let lastIsAsync;
let subscriber;
let didWarnUnstableReference;

const createSubscriber = ( stores ) => {
// The set of stores the `subscribe` function is supposed to subscribe to. Here it is
Expand Down Expand Up @@ -134,6 +135,24 @@ function Store( registry, suspense ) {
listeningStores
);

if ( process.env.NODE_ENV === 'development' ) {
if ( ! didWarnUnstableReference ) {
const secondMapResult =
registry.__unstableMarkListeningStores(
() => mapSelect( select, registry ),
listeningStores
);

if ( ! isShallowEqual( mapResult, secondMapResult ) ) {
// eslint-disable-next-line no-console
console.warn(
`The values returned by the 'useSelect' hook aren't referentially stable. This can lead to unnecessary rerenders.`
);
didWarnUnstableReference = true;
}
}
}

if ( ! subscriber ) {
subscriber = createSubscriber( listeningStores.current );
} else {
Expand Down

0 comments on commit 39b92c6

Please sign in to comment.