Skip to content
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

Support cloneElement in RSC-rendered <Link /> (better compatibility with Radix Primitives & friends) #1271

Open
3 tasks done
mschinis opened this issue Aug 19, 2024 · 4 comments

Comments

@mschinis
Copy link

Description

I'm trying to use shadcn NavigationMenu together with the next-intl link, using legacyBehavior+passHref but I'm getting a bunch of the following warnings:

"onClick" was passed to <Link> with `href` of `/about` but "legacyBehavior" was set. The legacy behavior requires onClick be set on the child of next/link 

Usage

// Import next-intl Link component returned by createSharedPathnamesNavigation
import { Link } from "@/i18n/navigation"; 

function NavigationMenuLocalizedLink({ item }: { item: INavigationMenuItem }) {
  return (
    <Link href={item.href} legacyBehavior passHref>
      <NavigationMenuLink>{item.title}</NavigationMenuLink>
    </Link>
  );
}

Tracing back the issue, it looks like nextjs doesn't like setting the onClick property, when legacyBehavior is set.

Verifications

  • I've verified that the problem I'm experiencing isn't covered in the docs.
  • I've searched for similar, existing issues on GitHub and Stack Overflow.
  • I've compared my app to a working example to look for differences.

Mandatory reproduction URL

https://github.com/mschinis/next-intl-bug-repro-app-router

Reproduction description

Steps to reproduce:

  1. Open reproduction
  2. Look at console

Expected behaviour

Expected behavior is to not have any warnings.

@mschinis mschinis added bug Something isn't working unconfirmed Needs triage. labels Aug 19, 2024
@mschinis mschinis changed the title onClick" was passed to <Link> with href Using Link component with legacyBehavior+passHref producing warnings Aug 19, 2024
@mschinis
Copy link
Author

mschinis commented Aug 19, 2024

The following workaround seems to work for now, but the behavior of the issue still stands:

'use client';

function NavigationMenuLocalizedLink({ item }: { item: INavigationMenuItem }) {
  return (
    <NavigationMenuLink asChild>
      <Link href={item.href}>{item.title}</Link>
    </NavigationMenuLink>
  );
}

@amannn
Copy link
Owner

amannn commented Aug 20, 2024

Thanks for the careful report and reproduction!

This could probably be addressed by adding cloneElement in BaseLink in case legacyBehavior is detected. I'm not sure though if it's worth it though, given that—as the name suggests—this is legacy behavior and there are modern alternatives (like the one you suggested).

This was in fact the first time this was reported, so maybe it's a good idea to keep this open to see if more people are affected and to give visibility to the alternative you've posted.

@amannn amannn added has-workaround and removed unconfirmed Needs triage. labels Aug 20, 2024
@rogerogers
Copy link

Yes, I'm also facing the same issue. Directly importing from next/link does not have this problem.

@amannn
Copy link
Owner

amannn commented Sep 5, 2024

This topic came up again in #1322 and I've added some details in #1322 (comment).

Will have a look if we can improve something here.

Btw. possibly a non-obtrusive workaround might be this:

// components/Link.tsx
'use client';

// Import from the file that creates the `<Link />` component
export {Link as default} from '@/routing';
// Import the re-exported `Link`
import Link from "components/Link"; 

function NavigationMenuLocalizedLink({ item }: { item: INavigationMenuItem }) {
  return (
    <Link href={item.href} legacyBehavior passHref>
      <NavigationMenuLink>{item.title}</NavigationMenuLink>
    </Link>
  );
}

I think this should work as expected with Radix Primitives & friends.

EDIT: Asked the Radix team here about an alternative implementation that would work with RSC: radix-ui/primitives#2537 (comment).

@amannn amannn changed the title Using Link component with legacyBehavior+passHref producing warnings Support cloneElement in RSC-rendered <Link /> (better compatibility with Radix Primitives & friends) Sep 5, 2024
@amannn amannn added enhancement New feature or request area: ergonomics and removed bug Something isn't working labels Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants