Skip to content
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

Optimize useSelect calls (batch 1) #23255

Merged
merged 1 commit into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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