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
useBlockTypesState: divide useSelect call into two #57163
Conversation
Size Change: -15 B (0%) Total Size: 1.71 MB
ℹ️ View Unchanged
|
const [ items ] = useSelect( | ||
( select ) => [ | ||
select( blockEditorStore ).getInserterItems( rootClientId ), | ||
], |
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.
Nit: I think we can just return items
here as we're dealing with a single collection now.
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.
Then useSelect
would shallow compare all the elements on each call, and that's wasted time when it can just compare oldItems === newItems
once, on the memoized array.
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.
I thought it would bail out early if oldItems === newItems
and avoid shallow comparison.
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.
I thought it would bail out early if
oldItems === newItems
and avoid shallow comparison.
@Mamaduka you're right! @wordpress/is-shallow-equal
really does a ===
comparison first and returns early.
But I think I'll keep the [ items ]
form anyway, for the sake of consistency 🙂
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.
Makes sense. Thank you, @jsnajdr!
A little optimization, or maybe just dividing one complex functions into two simpler ones. The
categories
andcollections
are constants, they don't change whenrootClientId
changes or whenblock-editor
store is updated. Avoid calling thegetCategories/Collections
selectors when not needed.