Refactor: Convert `withGlobalEvents` HOC to functional component
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
Wrapperclass to a functional component. - Used
useEffectto replacecomponentDidMountandcomponentWillUnmount. - Replaced
this.wrappedRefwithuseRef. - 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.
: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.
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.