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:
cc @nerrad @epiqueras
Ya, useSelect is doing a isShallowEqualObjects comparison: https://github.com/WordPress/gutenberg/blob/a894b3520598eceb91ddd8bb0299024b4f970afa/packages/data/src/components/use-select/index.js#L132-L134
So the question then is:
mapSelect callback should return an object ORuseSelect 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.
I'd go with the latter as well. We should have a shallow comparison helper that handles all data types.
I probably will find some time to do that this week.
Most helpful comment
I'd go with the latter as well. We should have a shallow comparison helper that handles all data types.