-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Lodash: Remove _.get()
for post type usages
#48121
Conversation
Size Change: +480 B (0%) Total Size: 1.33 MB
ℹ️ View Unchanged
|
Flaky tests detected in 94c367f. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4232858318
|
@@ -35,7 +30,7 @@ export default function DevicePreview() { | |||
hasActiveMetaboxes: select( editPostStore ).hasMetaBoxes(), | |||
isSaving: select( editPostStore ).isSavingMetaBoxes(), | |||
isPostSaveable: select( editorStore ).isEditedPostSaveable(), | |||
isViewable: get( postType, [ 'viewable' ], false ), | |||
isViewable: postType?.viewable, |
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.
All instances in this file work with a potentially nullish postType
, which is why I've added some optional chaining and nullish coalescing where necessary.
I've removed the default false
values in a few places where the value is already used as a boolean, because it won't make a difference.
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.
Defaulting to false
still makes sense, because it can avoid unnecessary rerenders. If getPostType()
call triggers a REST request, then postType?.viewable
is initially undefined
, and then becomes either true
or false
. The transition from undefined
to false
doesn't lead to any UI change, but yet it means a change of useSelect
return value and triggers a wasted rerender.
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.
Point taken, got the false
instances back in bfdd842.
bed93a3
to
6d7b435
Compare
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.
👍 Pointing out that defaulting to false
can be useful at some places, plus some stylistic nits.
@@ -35,7 +30,7 @@ export default function DevicePreview() { | |||
hasActiveMetaboxes: select( editPostStore ).hasMetaBoxes(), | |||
isSaving: select( editPostStore ).isSavingMetaBoxes(), | |||
isPostSaveable: select( editorStore ).isEditedPostSaveable(), | |||
isViewable: get( postType, [ 'viewable' ], false ), | |||
isViewable: postType?.viewable, |
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.
Defaulting to false
still makes sense, because it can avoid unnecessary rerenders. If getPostType()
call triggers a REST request, then postType?.viewable
is initially undefined
, and then becomes either true
or false
. The transition from undefined
to false
doesn't lead to any UI change, but yet it means a change of useSelect
return value and triggers a wasted rerender.
@@ -83,8 +82,8 @@ export function PageAttributesParent() { | |||
[ fieldValue ] | |||
); | |||
|
|||
const isHierarchical = get( postType, [ 'hierarchical' ], false ); |
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.
Here we could just return isHiearchical
(and parentPageLabel
, too) from the useSelect
callback few lines above. It already computes it.
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.
Good idea, reused in bfdd842.
@@ -105,7 +100,7 @@ function PostFeaturedImage( { | |||
const mediaUpload = useSelect( ( select ) => { | |||
return select( blockEditorStore ).getSettings().mediaUpload; | |||
}, [] ); | |||
const postLabel = get( postType, [ 'labels' ], {} ); |
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.
Instead of defaulting to {}
, you could write postLabels?.featured_image
at the places where the object is used. It's more consistent with how postType?.labels?.whatever
is used at other places, many of which are modified in this PR.
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.
Sure, done as part of bfdd842
What?
This PR removes Lodash's
_.get()
from a few use cases that work with the result ofgetPostType()
selector. Most of the usages affected here are similar and that makes it easier to review as a group.This PR compliments #48104.
Why?
Lodash is known to unnecessarily inflate the bundle size of packages, and in most cases, it can be replaced with native language functionality. See these for more information and rationale:
@wordpress/api-fetch
package haslodash
as a dependency #39495How?
We're using direct access with optional chaining and nullish coalescing as an alternative.
Testing Instructions