primitives
primitives copied to clipboard
[Slot] Access ref from props else fallback to element.ref
Description
fix #2769
Starting from the React 18.3.0 canary, accessing the ref via element.ref will throw a warning, and element.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.
Just chiming in to say that I had an issue with Portal when used in conjunction with framer-motion in the React canary releases since 19.0.0-canary-7a2609eed-20240403 and that this was the fix. Thank you.
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.
I also had to patch @radix-ui/react-presence to get rid of all warnings:
I also had to patch
@radix-ui/react-presenceto get rid of all warnings:
Ah thx, wasn’t able to find the others, will include this as well.
@juliusmarminge I applied the same fix to @radix-ui/react-presence too.
Just a gentle reminder that this PR being merged and released is so valuable for a lot of people. Thank you.
Would be great to get an approval from one of the code owners on this.. it also impacts Shadcn/ui
hey @andy-hook @benoitgrelard
hoping this gets approved soon. thanks!
As this is taking a little while, here's how to suppress the annoying logs until its merged:
_supressLogs.ts
// 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. For Next.js this would be
Root layout.tsx
import "./_supressLogs";
// Your root layout
And Root client provider file _Providers.tsx
"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 https://github.com/radix-ui/primitives/pull/2934
the new version is out and resolves the ref issue 🪩
