-
Notifications
You must be signed in to change notification settings - Fork 872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Slot] Access ref from props else fallback to element.ref #2811
Conversation
Just chiming in to say that I had an issue with Portal when used in conjunction with |
Thanks for fixing this |
Thank you! |
Is there a way to fix this manually / locally until the official fix is out? The 7x 'element.ref' warnings I am getting on each page refresh are very annoying. |
@juliusmarminge I applied the same fix to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Just a gentle reminder that this PR being merged and released is so valuable for a lot of people. Thank you. |
const ref = useComposedRefs(presence.ref, (child as any).ref); | ||
// Accessing the ref from props, else fallback to element.ref | ||
// https://github.com/facebook/react/pull/28348 | ||
const childrenRef = child.props.ref ?? (child as any).ref; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine to access child.ref
conditionally. React only warns when accessing child.ref
iff a ref
prop is actually passed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Would be great to get an approval from one of the code owners on this.. it also impacts Shadcn/ui |
hoping this gets approved soon. thanks! |
As this is taking a little while, here's how to suppress the annoying logs until its merged:
// TODO - delete when https://github.com/radix-ui/primitives/pull/2811 gets merged
const prevConsoleError = console.error;
const prevConsoleWarn = console.warn;
console.error = (...args) => {
if (args[0].includes("Warning: Accessing element.ref")) {
return;
}
prevConsoleError(...args);
};
console.warn = (...args) => {
if (args[0].includes("Warning: Accessing element.ref")) {
return;
}
prevConsoleWarn(...args);
}; Import this globally at your app root. Root import "./_supressLogs";
// Your root layout And Root client provider file "use client";
import "./_supressLogs";
// Your provider |
lgtm |
Please stop unnecessarily approving this PR. It adds noise to anyone who is subscribed. As a maintainer of OSS stuff myself, I can guarantee you, that adding noise will not help push things forward. Maintainers will get to it when they have time, they are most certainly well aware that this is a desired issue/PR. |
Thanks for the contribution @chungweileong94. This will be integrated altogether with the other changes required to support React 19 in #2934 |
the new version is out and resolves the |
- This prevents a warning coming up for handling refs: radix-ui/primitives#2811
Hello guys. I still get this error: Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release. Is 1.1.0 the last version? I have a nextjs 15 project |
That should be the latest version that contains the fix. If you are using component from shadcn ui, you will need to manually remove all the |
Thanks @chungweileong94. I am indeed using shadcn-ui. It is complaining about this file node_modules/@radix-ui/react-menu/node_modules/@radix-ui/react-slot/dist/packages/react/slot/src/Slot.tsx (66:71) but i can't locate it |
I looks like a dependency problem to me, have you try to update all the radix packages to the latest version? |
i did try to update. No success. Also i get this from time to time: |
@louishugens @chungweileong94 I think that the problem is the https://github.com/radix-ui/primitives/blob/main/packages/react/slot/src/Slot.tsx |
It's been awhile since I last created this PR, but as far as I can remember, the warning that React was throwing isn't about |
in an attempt to update to react 18.3, i am seeing exactly this issue. i was under the impression that edit: this is the error i am seeing:
|
I don't think React 18.3 will suffer the same problem though. But mind if you can provide a small repro for the error that you are facing? |
it appears after explicitly updating to |
The problem still exists. |
Description
fix #2769
Starting from the React 18.3.0 canary, accessing the ref via
element.ref
will throw a warning, andelement.ref
will be removed in the future React release.Instead, we should access the ref from props. To maintain the backward compatibility, we can do
children.props.ref ?? children.ref
.