From 3586560c8181b381e1b06ebf701bb8407c42d95f Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 28 Apr 2022 20:57:39 +0400 Subject: [PATCH] Navigation Link: Avoid unnecessary re-renders --- .../block-library/src/navigation-link/edit.js | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/packages/block-library/src/navigation-link/edit.js b/packages/block-library/src/navigation-link/edit.js index f5fc413c4893fc..fe629a533e7c94 100644 --- a/packages/block-library/src/navigation-link/edit.js +++ b/packages/block-library/src/navigation-link/edit.js @@ -342,7 +342,35 @@ function navStripHTML( html ) { * Add transforms to Link Control */ -function LinkControlTransforms( { block, transforms, replace } ) { +function LinkControlTransforms( { clientId, replace } ) { + const { getBlock, blockTransforms } = useSelect( + ( select ) => { + const { + getBlock: _getBlock, + getBlockRootClientId, + getBlockTransformItems, + } = select( blockEditorStore ); + + return { + getBlock: _getBlock, + blockTransforms: getBlockTransformItems( + [ _getBlock( clientId ) ], + getBlockRootClientId( clientId ) + ), + }; + }, + [ clientId ] + ); + + const featuredBlocks = [ + 'core/site-logo', + 'core/social-links', + 'core/search', + ]; + const transforms = blockTransforms.filter( ( item ) => { + return featuredBlocks.includes( item.name ); + } ); + if ( ! transforms?.length ) { return null; } @@ -359,8 +387,11 @@ function LinkControlTransforms( { block, transforms, replace } ) { key={ `transform-${ index }` } onClick={ () => replace( - block.clientId, - switchToBlockType( block, item.name ) + clientId, + switchToBlockType( + getBlock( clientId ), + item.name + ) ) } className="link-control-transform__item" @@ -424,12 +455,9 @@ export default function NavigationLinkEdit( { hasDescendants, userCanCreatePages, userCanCreatePosts, - thisBlock, - blockTransforms, } = useSelect( ( select ) => { const { - getBlock, getBlocks, getBlockName, getBlockRootClientId, @@ -437,7 +465,6 @@ export default function NavigationLinkEdit( { hasSelectedInnerBlock, getSelectedBlockClientId, getBlockParentsByBlockName, - getBlockTransformItems, } = select( blockEditorStore ); const selectedBlockId = getSelectedBlockClientId(); @@ -475,11 +502,6 @@ export default function NavigationLinkEdit( { 'create', 'posts' ), - thisBlock: getBlock( clientId ), - blockTransforms: getBlockTransformItems( - [ getBlock( clientId ) ], - getBlockRootClientId( clientId ) - ), }; }, [ clientId ] @@ -506,15 +528,6 @@ export default function NavigationLinkEdit( { replaceBlock( clientId, newSubmenu ); } - const featuredBlocks = [ - 'core/site-logo', - 'core/social-links', - 'core/search', - ]; - const featuredTransforms = blockTransforms.filter( ( item ) => { - return featuredBlocks.includes( item.name ); - } ); - useEffect( () => { // Show the LinkControl on mount if the URL is empty // ( When adding a new menu item) @@ -860,10 +873,7 @@ export default function NavigationLinkEdit( { ! url ? () => ( )