Skip to content

Commit

Permalink
Optimize useSelect calls. (#23255)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZebulanStanphill authored Jun 26, 2020
1 parent 76ef596 commit 153817e
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ export default function AutoBlockUninstaller() {
const shouldRemoveBlockTypes = useSelect( ( select ) => {
const { isAutosavingPost, isSavingPost } = select( 'core/editor' );
return isSavingPost() && ! isAutosavingPost();
} );
}, [] );

const unusedBlockTypes = useSelect( ( select ) =>
select( 'core/block-directory' ).getUnusedBlockTypes()
const unusedBlockTypes = useSelect(
( select ) => select( 'core/block-directory' ).getUnusedBlockTypes(),
[]
);

useEffect( () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { useSelect } from '@wordpress/data';
import CompactList from '../../components/compact-list';

export default function InstalledBlocksPrePublishPanel() {
const newBlockTypes = useSelect( ( select ) =>
select( 'core/block-directory' ).getNewBlockTypes()
const newBlockTypes = useSelect(
( select ) => select( 'core/block-directory' ).getNewBlockTypes(),
[]
);

if ( ! newBlockTypes.length ) {
Expand Down
8 changes: 4 additions & 4 deletions packages/block-editor/src/components/block-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export default function BlockActions( {
getBlocksByClientId,
getTemplateLock,
} = useSelect( ( select ) => select( 'core/block-editor' ), [] );
const {
getDefaultBlockName,
getGroupingBlockName,
} = useSelect( ( select ) => select( 'core/blocks' ) );
const { getDefaultBlockName, getGroupingBlockName } = useSelect(
( select ) => select( 'core/blocks' ),
[]
);

const blocks = getBlocksByClientId( clientIds );
const rootClientId = getBlockRootClientId( clientIds[ 0 ] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function BlockContextualToolbar( { focusOnMount, ...props } ) {
selectedBlockClientId &&
getBlockType( getBlockName( selectedBlockClientId ) ),
};
} );
}, [] );
if ( blockType ) {
if ( ! hasBlockSupport( blockType, '__experimentalToolbar', true ) ) {
return null;
Expand Down
6 changes: 2 additions & 4 deletions packages/block-editor/src/components/block-list/block-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import {

function BlockHTML( { clientId } ) {
const [ html, setHtml ] = useState( '' );
const { block } = useSelect(
( select ) => ( {
block: select( 'core/block-editor' ).getBlock( clientId ),
} ),
const block = useSelect(
( select ) => select( 'core/block-editor' ).getBlock( clientId ),
[ clientId ]
);
const { updateBlock } = useDispatch( 'core/block-editor' );
Expand Down
5 changes: 3 additions & 2 deletions packages/block-editor/src/components/block-preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ export function BlockPreview( {
__experimentalLive = false,
__experimentalOnClick,
} ) {
const settings = useSelect( ( select ) =>
select( 'core/block-editor' ).getSettings()
const settings = useSelect(
( select ) => select( 'core/block-editor' ).getSettings(),
[]
);
const renderedBlocks = useMemo( () => castArray( blocks ), [ blocks ] );
if ( ! blocks || blocks.length === 0 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ function UncontrolledInnerBlocks( props ) {
horizontalAlignment,
} = props;

const block = useSelect( ( select ) =>
select( 'core/block-editor' ).getBlock( clientId )
const block = useSelect(
( select ) => select( 'core/block-editor' ).getBlock( clientId ),
[ clientId ]
) || { innerBlocks: [] };

useNestedSettingsUpdate( clientId, allowedBlocks, templateLock );
Expand Down
18 changes: 9 additions & 9 deletions packages/block-editor/src/components/inserter/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ function InserterLibrary( {
__experimentalSelectBlockOnInsert: selectBlockOnInsert,
onSelect = noop,
} ) {
const { destinationRootClientId } = useSelect( ( select ) => {
const { getBlockRootClientId } = select( 'core/block-editor' );
const destinationRootClientId = useSelect(
( select ) => {
const { getBlockRootClientId } = select( 'core/block-editor' );

rootClientId =
rootClientId || getBlockRootClientId( clientId ) || undefined;

return {
rootClientId,
};
} );
return (
rootClientId || getBlockRootClientId( clientId ) || undefined
);
},
[ clientId, rootClientId ]
);

return (
<InserterMenu
Expand Down

0 comments on commit 153817e

Please sign in to comment.