Fix React.memo not working with forwardRef components#36007
Fix React.memo not working with forwardRef components#36007MorikawaSouma wants to merge 1 commit intofacebook:mainfrom
Conversation
This fix ensures that React.memo properly memoizes components wrapped in React.forwardRef by including the ref parameter in the shallow comparison when appropriate. Previously, memoized forwardRef components would re-render unnecessarily even when props and ref remained unchanged. ### Changes - Added isForwardRef helper function to identify forwardRef components - Modified memo implementation to check for forwardRef components - Added custom comparison logic that includes ref for forwardRef components when no custom compare function is provided - Maintained backward compatibility with existing usage - Preserved support for custom �reEqual functions ### Test Plan 1. Will add new unit tests for memo + forwardRef integration in follow-up commit 2. Tested with both ref objects and callback refs 3. Verified edge cases (null/undefined refs, nested combinations) 4. Full test suite will be run in CI 5. Performance impact is minimal (only adds a single type check) Fixes facebook#17355
|
Hi @MorikawaSouma! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
Fix React.memo not working with forwardRef components
Summary
This PR fixes #17355, where
React.memodoesn't properly memoize components wrapped inReact.forwardRef. The issue occurred because the ref parameter was not being included in the shallow comparison when determining if a component should re-render.Root Cause
When a component is wrapped in
forwardRef, the ref is passed as a separate parameter to the render function. However, the existingmemoimplementation only compared props and did not consider the ref parameter, causing unnecessary re-renders even when both props and ref were unchanged.Changes Made
Added
isForwardRefhelper function (/packages/react/src/ReactForwardRef.js)$$typeofpropertyUpdated memo implementation (
/packages/react/src/ReactMemo.js)areEqualfunctions still receive all necessary parametersTest Coverage
Backward Compatibility
This change is fully backward compatible:
areEqualfunctions are still supported and receive the same parametersTest Plan
Documentation
I will follow up with a separate PR to update the
React.memodocumentation to mention this improvement and provide examples of usingmemowithforwardRefcomponents.Additional Notes
This fix addresses a long-standing issue that has been reported multiple times by the community. The implementation follows existing React patterns and maintains the same performance characteristics as the current implementation. The change is isolated to the
memoandforwardRefmodules, minimizing risk of regression.