primitives icon indicating copy to clipboard operation
primitives copied to clipboard

[Slot] Access ref from props else fallback to element.ref

Open chungweileong94 opened this issue 1 year ago • 8 comments

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.

chungweileong94 avatar Apr 02 '24 14:04 chungweileong94

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.

ZipBrandon avatar Apr 06 '24 14:04 ZipBrandon

Thanks for fixing this

alaney2 avatar Apr 10 '24 00:04 alaney2

Thank you!

borispoehland avatar Apr 10 '24 07:04 borispoehland

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.

tillka avatar Apr 26 '24 12:04 tillka

I also had to patch @radix-ui/react-presence to get rid of all warnings:

CleanShot 2024-04-26 at 21 31 55@2x

juliusmarminge avatar Apr 26 '24 19:04 juliusmarminge

I also had to patch @radix-ui/react-presence to get rid of all warnings:

CleanShot 2024-04-26 at 21 31 55@2x

Ah thx, wasn’t able to find the others, will include this as well.

chungweileong94 avatar Apr 26 '24 23:04 chungweileong94

@juliusmarminge I applied the same fix to @radix-ui/react-presence too.

chungweileong94 avatar Apr 27 '24 07:04 chungweileong94

Just a gentle reminder that this PR being merged and released is so valuable for a lot of people. Thank you.

tungv avatar May 06 '24 13:05 tungv

Would be great to get an approval from one of the code owners on this.. it also impacts Shadcn/ui

Brenndoerfer avatar May 25 '24 20:05 Brenndoerfer

hey @andy-hook @benoitgrelard

hoping this gets approved soon. thanks!

paplco avatar May 27 '24 12:05 paplco

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

sannajammeh avatar May 28 '24 20:05 sannajammeh

lgtm

bcanfield avatar May 31 '24 12:05 bcanfield

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.

topaxi avatar Jun 03 '24 12:06 topaxi

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

vladmoroz avatar Jun 04 '24 12:06 vladmoroz

the new version is out and resolves the ref issue 🪩

mooxl avatar Jun 20 '24 15:06 mooxl