英文:
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
英文:
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
答案1
得分: 1
usePathname
钩子
从 Nextjs 13 开始,大部分 API 都发生了变化,包括路由 API。
现在要获取 pathName
,你需要使用 'next/navigation'
中的 usePathname
,如文档所示:
> usePathname
是一个客户端组件钩子,允许你读取当前 URL 的路径名。
> jsx > 'use client' > > import { usePathname } from 'next/navigation' > > export default function ExampleClientComponent() { > const pathname = usePathname() > return <>{pathname}</>; > } >
附言
同样适用于访问搜索参数,有两个新的 API useSearchParams 和 useParams。
英文:
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 'next/navigation'
as shown in the doc:
>usePathname
is a Client Component hook that lets you read the
> current URL's pathname.
>jsx
> 'use client'
>
> import { usePathname } from 'next/navigation'
>
> export default function ExampleClientComponent() {
> const pathname = usePathname()
> return <>Current pathname: {pathname}</>
> }
>
>
Aside
That goes also for accessing search params, 2 new API useSearchParams and useParams.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论