fix: getNodeRef should not fallback to element.ref in React 19#759
fix: getNodeRef should not fallback to element.ref in React 19#759GuinsooRocky wants to merge 2 commits intoreact-component:masterfrom
Conversation
Regression of react-component#593: propertyIsEnumerable('ref') returns false when the element has no ref prop, causing fallback to element.ref which triggers React 19's deprecation warning. Restore the version-based branch introduced in react-component#545.
|
@GuinsooRocky is attempting to deploy a commit to the React Component Team on Vercel. A member of the Team first needs to authorize it. |
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Walkthrough更新 getNodeRef:针对 ReactMajorVersion 分支以在 React 19+ 从 ele.props.ref 获取 ref(避免访问 ele.ref),并添加回归测试验证对无显式 ref 元素返回 null 且不触发 console.error。 变更React 19 Ref 兼容性
预估代码审查工作量🎯 2 (简单) | ⏱️ ~10 分钟 相关的 PR
建议的审查人员
诗
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request updates the getNodeRef utility to support React 19 by conditionally accessing ref from props instead of the element itself, which prevents deprecation warnings. A regression test was also added to verify that elements without a ref do not trigger these warnings. The review feedback suggests applying the nullish coalescing operator to the legacy code path to ensure a consistent null return type and improve type safety for consumers of the utility.
|
cc @zombieJ — this is a regression of #593 (the 'props check instead of version check' refactor). |
Address @gemini-code-assist review on react-component#759: ensure both branches return `React.Ref<T> | null` for consumers of this utility, even on the legacy React (<19) path where `element.ref` could theoretically be undefined.
Summary
getNodeRefcurrently triggers React 19'sAccessing element.ref was removed in React 19warning for any element rendered without an explicitrefprop.Root cause
The current implementation (introduced in #593, replacing the original version-based branch from #545):
propertyIsEnumerable('ref')returnsfalsewhen the rendered JSX does not pass aref, so the code falls through toele.ref. Readingelement.refis exactly what React 19 deprecated.This means the warning fires for every consumer of
getNodeRefwhose children may or may not carry a ref — for examplerc-dialog,rc-drawer, and downstream components like antd's<Modal />/<Drawer />.Fix
Restore the version-based branch that PR #545 originally used. On React 19+ read
props.ref; on older React keep readingelement.ref.ReactMajorVersionis already declared at the top of the file (used bysupportRef), so the change is local and minimal.Repro
Any element passed through
getNodeRefwithout a ref prop:In real apps this surfaces every time a
<Modal>/<Drawer>/<Dialog>is rendered with children that don't forward refs.Test
Added a regression test in
tests/ref-19.test.tsxthat:<div />without a refgetNodeRef(node)returnsnullconsole.errorwas not calledThe existing
it('getNodeRef')test is unchanged.Related
中文摘要
getNodeRef在 React 19 下会对没有传refprop 的元素触发Accessing element.ref was removed in React 19警告。根因
当前实现(来自 #593,覆盖了 #545 引入时的版本分支判断):
propertyIsEnumerable('ref')在 JSX 没传ref时返回false,于是 fallback 读ele.ref—— 而element.ref正是 React 19 标记为 deprecated 的访问点。这导致所有使用
getNodeRef的组件(rc-dialog/rc-drawer/ antd<Modal />/<Drawer />等)只要 children 不显式带 ref,控制台就会冒红字。修法
把分支逻辑回退到 #545 引入时的"按 React 版本判断"。文件顶部已有的
ReactMajorVersion直接复用,改动只有这一处。测试
tests/ref-19.test.tsx增加一条回归测试:<div />(不传 ref)getNodeRef(node)返回nullconsole.error没被调用Summary by CodeRabbit
Bug Fixes
Tests