close
Skip to content

Refactor: useMemo on elements and useCallback is back on resetAllFilter#78329

Merged
ciampo merged 4 commits into
WordPress:trunkfrom
USERSATOSHI:refactor/use-memo-for-elements
May 15, 2026
Merged

Refactor: useMemo on elements and useCallback is back on resetAllFilter#78329
ciampo merged 4 commits into
WordPress:trunkfrom
USERSATOSHI:refactor/use-memo-for-elements

Conversation

@USERSATOSHI
Copy link
Copy Markdown
Contributor

@USERSATOSHI USERSATOSHI commented May 15, 2026

What?

Follow up PR based on #78048 (review)

Performance optimization: wrap elements array and resetAllFilter callback in memoization hooks to prevent unnecessary recreations on every render.

Why?

The elements array was being recreated on every render, and resetAllFilter was being recreated whenever elements changed. This causes unnecessary re-renders of dependent UI components. Memoizing both ensures they only update when their actual dependencies change.

How?

  • Hoisted useHasCaptionPanel, useHasButtonPanel, and useHasHeadingPanel hook calls to the component top level (they can't be called inside useMemo)
  • Wrapped elements in useMemo with dependency array [ showCaptionPanel, showButtonPanel, showHeadingPanel ]
  • Wrapped resetAllFilter in useCallback with dependency array [ elements ]

Use of AI Tools

None

@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label May 15, 2026
@USERSATOSHI
Copy link
Copy Markdown
Contributor Author

cc: @ciampo

@USERSATOSHI USERSATOSHI marked this pull request as ready for review May 15, 2026 09:33
@USERSATOSHI USERSATOSHI requested a review from ellatrix as a code owner May 15, 2026 09:33
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 15, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: USERSATOSHI <tusharbharti@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: ciampo <mciampini@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mamaduka
Copy link
Copy Markdown
Member

Are there any visible wins for this memoization, or is it just theoretical? Is it expensive to recreate these values?

@ciampo
Copy link
Copy Markdown
Contributor

ciampo commented May 15, 2026

@Mamaduka actually has a point. The memoization doesn't really seem to bring concrete advantages:

  • the objects / functions being memoized are simple to re-recreate
  • the immediate consumer (ie. ColorToolsPanel) wraps that resetAllFilter function in a new, un-memoized callback anyway
  • the receiver component is not wrapper in React.memo
  • the underlying ToolsPanelItem component explicitly discards that stability (see code)

With that in mind, there is still one part of this PR that is a nice improvement, ie. hoisting the use*Panel calls

const showCaptionPanel = useHasCaptionPanel( settings );
const showButtonPanel = useHasButtonPanel( settings );
const showHeadingPanel = useHasHeadingPanel( settings );

We could either close this PR entirely, or keep it with only these minor changes.

@Mamaduka @USERSATOSHI , do you have a preference?

@Mamaduka
Copy link
Copy Markdown
Member

Yes, let’s keep panel status vars and discard memorization.

@Mamaduka Mamaduka added the [Type] Code Quality Issues or PRs that relate to code quality label May 15, 2026
@USERSATOSHI
Copy link
Copy Markdown
Contributor Author

@USERSATOSHI , do you have a preference?

The only reason I shared this issue was that the warning message was bugging me every time I edited that file. 😅

Copy link
Copy Markdown
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@ciampo ciampo merged commit 40e0941 into WordPress:trunk May 15, 2026
40 checks passed
@github-actions github-actions Bot added this to the Gutenberg 23.3 milestone May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Block editor /packages/block-editor [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants