Recently I started learning nextjs (13) and have this problem with navbar. The problem is that when I move through navbar to other section it's not affect the state and not adding class to that Link component so that the border of picked section display (that means we are on that section). When click second time on the same section it shows. I think it's all about server-side-rendering ofc.
const [selected, setSelected] = useState();
const click = (option) => {
setSelected(option);
};
return (
<div className="flex text-center flex-row navbar font-mono">
<div className="navLink">
<Link
onClick={() => click("popular")}
className={`option ${selected === "popular" ? "clicked" : ""} p-1`}
href={`/most_popular`}
>
Most Popular
</Link>
</div>
<div className="navLink">
<Link
onClick={() => click("rated")}
className={`option ${selected === "rated" ? "clicked" : ""} p-1`}
href={`/top_rated`}
>
Top Rated
</Link>
</div>
<div className="navLink">
<Link
onClick={() => click("upcoming")}
className={`option ${selected === "upcoming" ? "clicked" : ""} p-1`}
href={`/upcoming`}
>
Upcoming
</Link>
</div>
</div>
);
};
I am doing this simple, but what we have after clicking on specify Link it shows the server generated page, which is navBar without class clicked.
I was trying to use such a hooks like useRouter (next/navigation instead next/router), but we cannot get the current pathname now from it.