Fix refs validation for forwarded ref props#36208
Fix refs validation for forwarded ref props#36208fr3y123 wants to merge 3 commits intofacebook:mainfrom
Conversation
|
Hi @fr3y123! 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! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Fixes #35997
Summary
This fixes a false positive in
react-hooks/refswhen forwarding arefprop directly to JSX.Previously, code like:
was reported as:
Cannot access refs during rendereven though the component was only forwarding the ref object to JSX and not reading
ref.current.The root cause was that
ValidateNoRefAccessInRendervalidated all JSX attribute operands uniformly. That was too strict for the specialrefprop, where passing a ref object through to JSX should be allowed.This change teaches the validator to treat JSX
refattributes specially:refJSX attributesrefattribute itselfThis preserves the existing error for actual render-time ref reads like
props.ref.current, while allowing valid ref forwarding patterns.How did you test this change?
Reproduced the issue with a focused compiler fixture:
Before the fix, this failed with:
After the fix, the same targeted fixture passes:
I also checked the existing invalid fixture for reading
props.ref.currentto confirm the validator still rejects direct ref-value access during render.