From c70dfe39c872e321309a4341c12800c629e57274 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 19 Sep 2023 13:25:20 +1200 Subject: [PATCH] Memoize the createPageEntity callback to stop useBlockEditorSettings firing on every keystroke --- .../provider/use-block-editor-settings.js | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/editor/src/components/provider/use-block-editor-settings.js b/packages/editor/src/components/provider/use-block-editor-settings.js index b430c3212556b..705374b727ea7 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { Platform, useMemo } from '@wordpress/element'; +import { Platform, useMemo, useCallback } from '@wordpress/element'; import { useDispatch, useSelect } from '@wordpress/data'; import { store as coreStore, @@ -182,14 +182,19 @@ function useBlockEditorSettings( settings, hasTemplate ) { * @param {Object} options parameters for the post being created. These mirror those used on 3rd param of saveEntityRecord. * @return {Object} the post type object that was created. */ - const createPageEntity = ( options ) => { - if ( ! userCanCreatePages ) { - return Promise.reject( { - message: __( 'You do not have permission to create Pages.' ), - } ); - } - return saveEntityRecord( 'postType', 'page', options ); - }; + const createPageEntity = useCallback( + ( options ) => { + if ( ! userCanCreatePages ) { + return Promise.reject( { + message: __( + 'You do not have permission to create Pages.' + ), + } ); + } + return saveEntityRecord( 'postType', 'page', options ); + }, + [ saveEntityRecord, userCanCreatePages ] + ); return useMemo( () => ( {