gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Refactor: Convert `withGlobalEvents` HOC to functional component

Open R1shabh-Gupta opened this issue 7 months ago • 2 comments

What?

See https://github.com/WordPress/gutenberg/issues/22890

This PR refactors the withGlobalEvents higher-order component (HOC) from a class-based implementation to a functional component using React hooks.

Why?

Class-based components are no longer the recommended approach in React. Functional components with hooks are preferred as they offer:

  • Better readability and simplicity
  • Easier maintainability and testing
  • Alignment with modern Gutenberg code standards

How?

  • Converted the internal Wrapper class to a functional component.
  • Used useEffect to replace componentDidMount and componentWillUnmount.
  • Replaced this.wrappedRef with useRef.
  • Preserved all original logic, including global listener binding and forwarded ref handling.

Testing Instructions

Run unit tests for the component:

npm run test:unit ./packages/compose/src/higher-order/with-global-events/test/index.js 
npm run test:unit ./packages/compose/src/higher-order/with-global-events/test/listener.js

Screenshots or screencast

N/A – this is a logic-only (non-visual) component.

R1shabh-Gupta avatar Jun 16 '25 07:06 R1shabh-Gupta

:wave: Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @R1shabh-Gupta! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

github-actions[bot] avatar Jun 16 '25 07:06 github-actions[bot]

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: R1shabh-Gupta <[email protected]>

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

github-actions[bot] avatar Jun 16 '25 17:06 github-actions[bot]