0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【React】MantineのメニューリンクでNext.js13以降のLinkを設定したい場合

Posted at

概要

MantineのMenuコンポーネントではMenu.Item as Next.js Linkのドキュメントで紹介されている通り、Next.jsのリンクを設定することができます。ただ、このドキュメントの記載(2025年3月時点)では、Next.js 13以降だとNext.js 13系で Link with a or use Link legacyBehavior エラーの解決方法の記事にある通り、aタグが配下にあることでエラーとなります。
ということで、どういうふうに対応すればよいかのメモ書きを残しておきます。

前提

  • 使用したNext.jsのバージョンは15.2.3です。
  • 使用したMantineのバージョンは7.17.2です。

対応

以下の通り、aタグを削除してothersのpropsとrefをLinkコンポーネントに直接指定すれば大丈夫でした。

import { forwardRef } from "react";
import Link from "next/link";

export const NextLink = forwardRef(
  (
    { href, ...others }: React.ComponentPropsWithoutRef<typeof Link>,
    ref: React.ForwardedRef<HTMLAnchorElement>
  ) => <Link href={href} {...others} ref={ref}></Link>
);
NextLink.displayName = "nextLink";
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?