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

useSelect doesn't return the updated value if it is not an object. #16612

Closed
garciaalvaro opened this issue Jul 16, 2019 · 4 comments · Fixed by #16669
Closed

useSelect doesn't return the updated value if it is not an object. #16612

garciaalvaro opened this issue Jul 16, 2019 · 4 comments · Fixed by #16669
Assignees
Labels
[Package] Data /packages/data [Type] Bug An existing feature does not function as intended

Comments

@garciaalvaro
Copy link

garciaalvaro commented Jul 16, 2019

Describe the bug
Following the documentation of useSelect hook to return a value from the store (custom or core) the updated value is only returned if the callback returns an object.

To reproduce
Try the following isolated. Notice that in the third example getSelectedBlock() is being returned as is.

Doesn't update
const is_typing = useSelect(select => select("core/block-editor").isTyping());

Updates correctly
const is_typing = useSelect(select => ({ value: select("core/block-editor").isTyping() }));

Updates correctly
const selected = useSelect(select => select("core/block-editor").getSelectedBlock());

Version:

  • WordPress: 5.2.2
  • Gutenberg 6.1.1
@youknowriad youknowriad added [Type] Bug An existing feature does not function as intended Needs Testing Needs further testing to be confirmed. labels Jul 16, 2019
@youknowriad
Copy link
Contributor

cc @nerrad @epiqueras

@youknowriad youknowriad added the [Package] Data /packages/data label Jul 16, 2019
@nerrad
Copy link
Contributor

nerrad commented Jul 16, 2019

Ya, useSelect is doing a isShallowEqualObjects comparison:

if ( isShallowEqualObjects( latestMapOutput.current, newMapOutput ) ) {
return;
}

So the question then is:

  • do we update documentation so that its clear the mapSelect callback should return an object OR
  • do we modify useSelect so that it will do shallow compares of arrays and primitive types as well?

I think the latter would be useful.

In addition: We should also make sure tests cover whatever we decide. If the latter, the existing unit tests will need updated to cover testing the mapSelect returning different data types.

@epiqueras
Copy link
Contributor

I'd go with the latter as well. We should have a shallow comparison helper that handles all data types.

@nerrad
Copy link
Contributor

nerrad commented Jul 16, 2019

I probably will find some time to do that this week.

@nerrad nerrad self-assigned this Jul 16, 2019
@nerrad nerrad removed the Needs Testing Needs further testing to be confirmed. label Jul 18, 2019
swissspidy added a commit to ampproject/amp-wp that referenced this issue Jul 24, 2019
Since it seems to generate a new array every time, strict equal fails, triggering a new component update.

To avoid this, return undefined when there are no block validation errors anyway.

Kind of related: WordPress/gutenberg#16612
swissspidy added a commit to ampproject/amp-wp that referenced this issue Jul 24, 2019
Since it seems to generate a new array every time, strict equal fails, triggering a new component update.

To avoid this, return undefined when there are no block validation errors anyway.

Kind of related: WordPress/gutenberg#16612
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Data /packages/data [Type] Bug An existing feature does not function as intended
Projects
None yet
4 participants