Skip to content

Commit

Permalink
Optimize useSelect calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZebulanStanphill committed Jun 25, 2020
1 parent e2b9bb2 commit 8615cd5
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 51 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
14 changes: 4 additions & 10 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,10 @@ function InserterMenu( {
isAppender,
selectBlockOnInsert: __experimentalSelectBlockOnInsert,
} );
const { hasPatterns } = useSelect(
( select ) => {
const { getSettings } = select( 'core/block-editor' );
return {
hasPatterns: !! getSettings().__experimentalBlockPatterns
?.length,
};
},
[ isAppender, clientId, rootClientId ]
);
const hasPatterns = useSelect( ( select ) => {
const { getSettings } = select( 'core/block-editor' );
return !! getSettings().__experimentalBlockPatterns?.length;
}, [] );

const showPatterns = ! destinationRootClientId && hasPatterns;
const onKeyDown = ( event ) => {
Expand Down
7 changes: 2 additions & 5 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,9 @@ function LinkControl( {
const [ errorMessage, setErrorMessage ] = useState( null );
const isEndingEditWithFocus = useRef( false );

const { fetchSearchSuggestions } = useSelect( ( select ) => {
const fetchSearchSuggestions = useSelect( ( select ) => {
const { getSettings } = select( 'core/block-editor' );
return {
fetchSearchSuggestions: getSettings()
.__experimentalFetchLinkSuggestions,
};
return getSettings().__experimentalFetchLinkSuggestions;
}, [] );
const displayURL =
( value && filterURLForDisplay( safeDecodeURI( value.url ) ) ) || '';
Expand Down
5 changes: 3 additions & 2 deletions packages/block-editor/src/components/observe-typing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ function isKeyDownEligibleForStartTyping( event ) {
function ObserveTyping( { children, setTimeout: setSafeTimeout } ) {
const typingContainer = useRef();
const lastMouseMove = useRef();
const isTyping = useSelect( ( select ) =>
select( 'core/block-editor' ).isTyping()
const isTyping = useSelect(
( select ) => select( 'core/block-editor' ).isTyping(),
[]
);
const { startTyping, stopTyping } = useDispatch( 'core/block-editor' );
useEffect( () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ const __experimentalPageTemplatePicker = ( {
visible,
} ) => {
const { editPost } = useDispatch( 'core/editor' );
const { title } = useSelect( ( select ) => {
const title = useSelect( ( select ) => {
const { getEditedPostAttribute } = select( 'core/editor' );

return {
title: getEditedPostAttribute( 'title' ),
};
} );
return getEditedPostAttribute( 'title' );
}, [] );

const [ templatePreview, setTemplatePreview ] = useState();
const [ pickerVisible, setPickerVisible ] = useState( visible );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import styles from './styles.scss';
const BlockPreview = ( { blocks } ) => {
const currentSettings = useSelect( ( select ) => {
return select( 'core/block-editor' ).getSettings();
} );
}, [] );
const settings = {
...currentSettings,
readOnly: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function SpacingPanelControl( { children, ...props } ) {
const isSpacingEnabled = useSelect( ( select ) => {
const { getSettings } = select( 'core/block-editor' );
return get( getSettings(), '__experimentalEnableCustomSpacing' );
} );
}, [] );

if ( ! isSpacingEnabled ) return null;

Expand Down

0 comments on commit 8615cd5

Please sign in to comment.