-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Conversation
Size Change: +354 B (0%) Total Size: 1.51 MB
ℹ️ View Unchanged
|
0600677
to
39b92c6
Compare
Okay, I think this is ready for initial review and testing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this looks good. Let me know what you think about the feedback, then we can merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also changelog entry would be nice. |
I'll add the changelog update. Should I keep PR open while fixing remaining core "violations"? The good news is that I've spotted at most ten. More general question: Would this enhancement be beneficial for third-party consumers? |
I'd prefer merging immediately. It's OK to have console warnings for some time while violations are fixed. Third party consumers should benefit. Just like everybody benefits from React warnings that are very similar in spirit to this one. |
Sorry forgot to add a changelog entry before merging this 😓 - #53685. |
Really cool addition 👍 |
I wonder if providing both Simple solution patch:diff --git packages/data/src/components/use-select/index.js packages/data/src/components/use-select/index.js
index 9afb4a6cfbd..3ff13229c47 100644
--- packages/data/src/components/use-select/index.js
+++ packages/data/src/components/use-select/index.js
@@ -161,7 +161,11 @@ function Store( registry, suspense ) {
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.`
+ `The 'useSelect' hook returns different values when called with the same state and parameters. This can lead to unnecessary rerenders.`,
+ {
+ prev: mapResult,
+ next: secondMapResult,
+ }
);
didWarnUnstableReference = true;
} Screenshot |
Logging which keys actually fail the shallow equality check would me much more useful. When there are two non-identical objects that contain identical data (e.g., from two calls of a non-memoized selector), looking at them in the console doesn't help, because they look the same. If I want to find out what the problem really is, I need to create some custom logging code like: console.log( 'non-equal keys:', Object.keys( a ).filter( ( k ) => a[ k ] !== b[ k ] ) ); Would be nice if this was built-in. |
What?
See #53596 (comment).
PR adds a warning for
useSelect
hooks that return a new object/array reference on eachmapSelecet
call. The warning will only be displayed in dev mode.Why?
This will help us to catch possible bugs like #53596 early in development.
How?
Call the
mapSelect
function again and compare the returned results with the previous call.Testing Instructions
Example plugin
Screenshots or screencast