[Navigation Menu] What is the proper way to use with next/link #1260
-
I am curious what the best way to use NavMenu.Link with next/link is. import type { FC } from "react";
import { Link as NavLink } from "@radix-ui/react-navigation-menu";
import Link, { LinkProps } from "next/link";
export const MainMenuControlLink: FC<LinkProps> = ({
children,
...props
}) => (
<Link shallow {...props} passHref>
<NavLink className="classes-for-my-link">
{children}
</NavLink>
</Link>
); It's weird that I need to use passHref for it to work when the NavLink (from radix-ui) is of type anchor. Which normally means I do not have to use passHref. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
I haven't played much with next, but from what I understand looking at the docs, you are doing the right thing and |
Beta Was this translation helpful? Give feedback.
-
I got this to work by wrapping the
Although I didn't yet figure how to set the active link using the example with router code so commented it out. Those don't work with nextjs link :
|
Beta Was this translation helpful? Give feedback.
-
I believe you now also need to use |
Beta Was this translation helpful? Give feedback.
-
Hi All - just in case anyone else comes across this - we solved this by creating a special We ended up doing this because of the way Radix menu items internally call We then placed an Hope this helps. |
Beta Was this translation helpful? Give feedback.
Hi @DanielBailey-web,
I haven't played much with next, but from what I understand looking at the docs, you are doing the right thing and
passHref
is needed becauseNavLink
is a custom component that wraps aa
.Source:
https://nextjs.org/docs/api-reference/next/link#if-the-child-is-a-custom-component-that-wraps-an-a-tag