Skip to content

Commit

Permalink
Optimize selector calls (#23930)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Jul 15, 2020
1 parent 2481ed4 commit 4d6f576
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
9 changes: 7 additions & 2 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useState, createContext, useMemo } from '@wordpress/element';
import {
useState,
createContext,
useMemo,
useCallback,
} from '@wordpress/element';
import {
getBlockType,
getSaveElement,
Expand Down Expand Up @@ -105,7 +110,7 @@ function BlockListBlock( {
[ clientId ]
);
const { removeBlock } = useDispatch( 'core/block-editor' );
const onRemove = () => removeBlock( clientId );
const onRemove = useCallback( () => removeBlock( clientId ), [ clientId ] );

// Handling the error state
const [ hasError, setErrorState ] = useState( false );
Expand Down
25 changes: 16 additions & 9 deletions packages/edit-post/src/components/sidebar/post-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,30 @@ function PostLink( {
onTogglePanel,
isEditable,
postLink,
permalinkParts,
permalinkPrefix,
permalinkSuffix,
editPermalink,
forceEmptyField,
setState,
postSlug,
postTypeLabel,
} ) {
const { prefix, suffix } = permalinkParts;
let prefixElement, postNameElement, suffixElement;
if ( isEditable ) {
prefixElement = prefix && (
<span className="edit-post-post-link__link-prefix">{ prefix }</span>
prefixElement = permalinkPrefix && (
<span className="edit-post-post-link__link-prefix">
{ permalinkPrefix }
</span>
);
postNameElement = postSlug && (
<span className="edit-post-post-link__link-post-name">
{ postSlug }
</span>
);
suffixElement = suffix && (
<span className="edit-post-post-link__link-suffix">{ suffix }</span>
suffixElement = permalinkSuffix && (
<span className="edit-post-post-link__link-suffix">
{ permalinkSuffix }
</span>
);
}

Expand Down Expand Up @@ -137,21 +141,24 @@ export default compose( [

const postTypeName = getEditedPostAttribute( 'type' );
const postType = getPostType( postTypeName );
const permalinkParts = getPermalinkParts();

return {
postLink: link,
isEditable: isPermalinkEditable(),
isPublished: isCurrentPostPublished(),
isOpened: isEditorPanelOpened( PANEL_NAME ),
permalinkParts: getPermalinkParts(),
isEnabled: isEditorPanelEnabled( PANEL_NAME ),
isViewable: get( postType, [ 'viewable' ], false ),
postSlug: safeDecodeURIComponent( getEditedPostSlug() ),
postTypeLabel: get( postType, [ 'labels', 'view_item' ] ),
hasPermalinkParts: !! permalinkParts,
permalinkPrefix: permalinkParts?.prefix,
permalinkSuffix: permalinkParts?.suffix,
};
} ),
ifCondition( ( { isEnabled, postLink, isViewable, permalinkParts } ) => {
return isEnabled && postLink && isViewable && permalinkParts;
ifCondition( ( { isEnabled, postLink, isViewable, hasPermalinkParts } ) => {
return isEnabled && postLink && isViewable && hasPermalinkParts;
} ),
withDispatch( ( dispatch ) => {
const { toggleEditorPanelOpened } = dispatch( 'core/edit-post' );
Expand Down

0 comments on commit 4d6f576

Please sign in to comment.