Skip to content

Commit

Permalink
Inserter: Return the same items when the state and parameters don't c…
Browse files Browse the repository at this point in the history
…hange (WordPress#62263)

* Inserter: Return the same items when the state and parameters don't change
* Use stable default

Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
  • Loading branch information
4 people authored and patil-vipul committed Jun 17, 2024
1 parent 715d1bf commit ab72bb2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
parse,
} from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
import { useCallback, useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -25,13 +25,18 @@ import { withRootClientIdOptionKey } from '../../../store/utils';
* @return {Array} Returns the block types state. (block types, categories, collections, onSelect handler)
*/
const useBlockTypesState = ( rootClientId, onInsert, isQuick ) => {
const options = useMemo(
() => ( { [ withRootClientIdOptionKey ]: ! isQuick } ),
[ isQuick ]
);
const [ items ] = useSelect(
( select ) => [
select( blockEditorStore ).getInserterItems( rootClientId, {
[ withRootClientIdOptionKey ]: ! isQuick,
} ),
select( blockEditorStore ).getInserterItems(
rootClientId,
options
),
],
[ rootClientId, isQuick ]
[ rootClientId, options ]
);

const [ categories, collections ] = useSelect( ( select ) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const EMPTY_ARRAY = [];
*/
const EMPTY_SET = new Set();

const EMPTY_OBJECT = {};

/**
* Returns a block's name given its client ID, or null if no block exists with
* the client ID.
Expand Down Expand Up @@ -1996,7 +1998,7 @@ const buildBlockTypeItem =
*/
export const getInserterItems = createRegistrySelector( ( select ) =>
createSelector(
( state, rootClientId = null, options = {} ) => {
( state, rootClientId = null, options = EMPTY_OBJECT ) => {
const buildReusableBlockInserterItem = ( reusableBlock ) => {
const icon = ! reusableBlock.wp_pattern_sync_status
? {
Expand Down

0 comments on commit ab72bb2

Please sign in to comment.