diff --git a/packages/components/src/higher-order/with-notices/index.js b/packages/components/src/higher-order/with-notices/index.js index f8e89f826f956e..eaa6d4c74c0d00 100644 --- a/packages/components/src/higher-order/with-notices/index.js +++ b/packages/components/src/higher-order/with-notices/index.js @@ -6,7 +6,7 @@ import { v4 as uuid } from 'uuid'; /** * WordPress dependencies */ -import { forwardRef, useState, useCallback } from '@wordpress/element'; +import { forwardRef, useState, useMemo } from '@wordpress/element'; import { createHigherOrderComponent } from '@wordpress/compose'; /** @@ -25,55 +25,53 @@ export default createHigherOrderComponent( ( OriginalComponent ) => { function Component( props, ref ) { const [ noticeList, setNoticeList ] = useState( [] ); - /** - * Function passed down as a prop that adds a new notice. - * - * @param {Object} notice Notice to add. - */ - const createNotice = useCallback( ( notice ) => { - const noticeToAdd = notice.id ? notice : { ...notice, id: uuid() }; - setNoticeList( ( current ) => [ ...current, noticeToAdd ] ); - }, [] ); + const noticeOperations = useMemo( () => { + /** + * Function passed down as a prop that adds a new notice. + * + * @param {Object} notice Notice to add. + */ + const createNotice = ( notice ) => { + const noticeToAdd = notice.id + ? notice + : { ...notice, id: uuid() }; + setNoticeList( ( current ) => [ ...current, noticeToAdd ] ); + }; - /** - * Function passed as a prop that adds a new error notice. - * - * @param {string} msg Error message of the notice. - */ - const createErrorNotice = useCallback( - ( msg ) => { - createNotice( { - status: 'error', - content: msg, - } ); - }, - [ createNotice ] - ); + return { + createNotice, - /** - * Removes a notice by id. - * - * @param {string} id Id of the notice to remove. - */ - const removeNotice = useCallback( ( id ) => { - setNoticeList( ( current ) => - current.filter( ( notice ) => notice.id !== id ) - ); - }, [] ); + /** + * Function passed as a prop that adds a new error notice. + * + * @param {string} msg Error message of the notice. + */ + createErrorNotice: ( msg ) => { + createNotice( { + status: 'error', + content: msg, + } ); + }, - /** - * Removes all notices - */ - const removeAllNotices = useCallback( () => { - setNoticeList( [] ); - }, [] ); + /** + * Removes a notice by id. + * + * @param {string} id Id of the notice to remove. + */ + removeNotice: ( id ) => { + setNoticeList( ( current ) => + current.filter( ( notice ) => notice.id !== id ) + ); + }, - const noticeOperations = { - createNotice, - createErrorNotice, - removeNotice, - removeAllNotices, - }; + /** + * Removes all notices + */ + removeAllNotices: () => { + setNoticeList( [] ); + }, + }; + }, [] ); const propsOut = { ...props,