Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data: Warn if the 'useSelect' hook returns different values when called with the same state and parameters #53666

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 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,19 @@ function Store( registry, suspense ) {
listeningStores
);

if ( process.env.NODE_ENV === 'development' ) {
if ( ! didWarnUnstableReference ) {
const secondMapResult = mapSelect( select, registry );
if ( ! isShallowEqual( mapResult, secondMapResult ) ) {
// eslint-disable-next-line no-console
console.warn(
`The 'useSelect' hook returns different values when called with the same state and parameters. This can lead to unnecessary rerenders.`
);
didWarnUnstableReference = true;
}
}
}

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