diff --git a/packages/core-data/src/selectors.js b/packages/core-data/src/selectors.js index 429fbab424fec2..7ea0cff6e4ebfb 100644 --- a/packages/core-data/src/selectors.js +++ b/packages/core-data/src/selectors.js @@ -18,6 +18,17 @@ import { getQueriedItems } from './queried-data'; import { DEFAULT_ENTITY_KEY } from './entities'; import { getNormalizedCommaSeparable } from './utils'; +/** + * Shared reference to an empty array for cases where it is important to avoid + * returning a new array reference on every invocation, as in a connected or + * other pure component which performs `shouldComponentUpdate` check on props. + * This should be used as a last resort, since the normalized data should be + * maintained by the reducer result in state. + * + * @type {Array} + */ +const EMPTY_ARRAY = []; + /** * Returns true if a request is in progress for embed preview data, or false * otherwise. @@ -238,7 +249,7 @@ export function getEntityRecords( state, kind, name, query ) { 'queriedData', ] ); if ( ! queriedState ) { - return []; + return EMPTY_ARRAY; } return getQueriedItems( queriedState, query ); } diff --git a/packages/core-data/src/test/selectors.js b/packages/core-data/src/test/selectors.js index 0da0e3c30df8e0..0ec684a21d33d6 100644 --- a/packages/core-data/src/test/selectors.js +++ b/packages/core-data/src/test/selectors.js @@ -247,6 +247,42 @@ describe( 'getEntityRecords', () => { { slug: 'page' }, ] ); } ); + + it( 'should return the same instance with the same arguments', () => { + let state = deepFreeze( { + entities: { + data: {}, + }, + } ); + + const postTypeFirstRecords = getEntityRecords( + state, + 'root', + 'postType' + ); + const wpBlockFirstRecords = getEntityRecords( + state, + 'postType', + 'wp_block' + ); + + // Simulate update states + state = { ...state }; + + const postTypeSecondRecords = getEntityRecords( + state, + 'root', + 'postType' + ); + const wpBlockSecondRecords = getEntityRecords( + state, + 'postType', + 'wp_block' + ); + + expect( postTypeFirstRecords ).toBe( postTypeSecondRecords ); + expect( wpBlockFirstRecords ).toBe( wpBlockSecondRecords ); + } ); } ); describe( '__experimentalGetDirtyEntityRecords', () => { diff --git a/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js b/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js index c29107a394093c..0fbc2959971119 100644 --- a/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js +++ b/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js @@ -46,7 +46,8 @@ export default function WidgetAreasBlockEditorProvider( { 'postType', 'wp_block' ), - } ) + } ), + [] ); const { setIsInserterOpened } = useDispatch( 'core/edit-widgets' );