NextJS 13.4,useRouter()不起作用 | 路径名未定义

huangapple go评论72阅读模式
英文:

NextJS 13.4, useRouter() is not working | pathname is undefined

问题

I am trying to make an active link and change the styling to underline when it's active. I am using useRouter() from 'next/navigation', because importing useRouter from 'next/router' gives an error. But my link is still not working.

Here's my code:

import React from 'react';
import Image from 'next/image';
import hamburgerMenu from '../../public/Assets/hamburger-menu.svg';
import { useRouter } from 'next/navigation';
import Link from 'next/link';

function HamburgerMenu({ isModalOpen, handleModal }) {
    const { pathname } = useRouter();

    return (
        <>
            {isModalOpen && <></>}
            <button onClick={handleModal}>
                <Image src={hamburgerMenu} width={20} height={14} />
            </button>
            {isModalOpen && (
                <div className='absolute top-16 left-[50%] translate-x-[-50%]'>
                    <ul>
                        <li>
                            <Link href='/' className={pathname == '/' ? 'underline' : ''}>
                                Home
                            </Link>
                        </li>
                        <li>
                            <Link
                                href='/about'
                                className={pathname == '/about' ? 'underline' : ''}>
                                About
                            </Link>
                        </li>
                    </ul>
                </div>
            )}
        </>
    );
}

export default HamburgerMenu;

This code is working for everyone except me NextJS 13.4,useRouter()不起作用 | 路径名未定义

英文:

I am trying to make an active link and change the styling to underline when it's active. I am using useRouter() from 'next/navigation', because importing useRouter from 'next/router' gives an error. But my link is still not working.

Here's my code:

import React from &#39;react&#39;;
import Image from &#39;next/image&#39;;
import hamburgerMenu from &#39;../../public/Assets/hamburger-menu.svg&#39;;
import { useRouter } from &#39;next/navigation&#39;;
import Link from &#39;next/link&#39;;

function HamburgerMenu({ isModalOpen, handleModal }) {
	const { pathname } = useRouter();

	return (
		&lt;&gt;
			{isModalOpen &amp;&amp; &lt;&gt;&lt;/&gt;}
			&lt;button onClick={handleModal}&gt;
				&lt;Image src={hamburgerMenu} width={20} height={14} /&gt;
			&lt;/button&gt;
			{isModalOpen &amp;&amp; (
				&lt;div className=&#39;absolute top-16 left-[50%] translate-x-[-50%]&#39;&gt;
					&lt;ul&gt;
						&lt;li&gt;
							&lt;Link href=&#39;/&#39; className={pathname == &#39;/&#39; ? &#39;underline&#39; : &#39;&#39;}&gt;
								Home
							&lt;/Link&gt;
						&lt;/li&gt;
						&lt;li&gt;
							&lt;Link
								href=&#39;/about&#39;
								className={pathname == &#39;/about&#39; ? &#39;underline&#39; : &#39;&#39;}&gt;
								About
							&lt;/Link&gt;
						&lt;/li&gt;
					&lt;/ul&gt;
				&lt;/div&gt;
			)}
		&lt;/&gt;
	);
}

export default HamburgerMenu;

This code is working for everyone except me NextJS 13.4,useRouter()不起作用 | 路径名未定义

答案1

得分: 1

usePathname 钩子

从 Nextjs 13 开始,大部分 API 都发生了变化,包括路由 API。

现在要获取 pathName,你需要使用 &#39;next/navigation&#39; 中的 usePathname,如文档所示:

> usePathname 是一个客户端组件钩子,允许你读取当前 URL 的路径名。
> jsx &gt; 'use client' &gt; &gt; import { usePathname } from 'next/navigation' &gt; &gt; export default function ExampleClientComponent() { &gt; const pathname = usePathname() &gt; return <>{pathname}</>; &gt; } &gt;

附言

同样适用于访问搜索参数,有两个新的 API useSearchParamsuseParams

英文:

usePathname hook

As of Nextjs 13 most of the api has changes include the router APIs.

Now to get the pathName, you need to use the usePathname from &#39;next/navigation&#39; as shown in the doc:

>usePathname is a Client Component hook that lets you read the
> current URL's pathname.
>jsx
&gt; &#39;use client&#39;
&gt;
&gt; import { usePathname } from &#39;next/navigation&#39;
&gt;
&gt; export default function ExampleClientComponent() {
&gt; const pathname = usePathname()
&gt; return &lt;&gt;Current pathname: {pathname}&lt;/&gt;
&gt; }
&gt;

>

Aside

That goes also for accessing search params, 2 new API useSearchParams and useParams.

huangapple
  • 本文由 发表于 2023年6月12日 16:19:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定