Hi there,
I tried to add styling to an active link in Nextjs and I read the documentation, but it doesn´t work for my app. Can someone please let me know whats wrong with my code?
import Link from 'next/link';
import { useRouter } from 'next/router';
export default function Navigation() {
const router = useRouter();
return (
<nav className="navigation">
<Link href="/">
<a className={router.pathname == '/' ? 'active' : ''}>HOME</a>
</Link>
<Link href="/link1">
<a className={router.pathname == '/' ? 'active' : ''}>LINK1</a>
</Link>
<Link href="/link2">
<a className={router.pathname == '/' ? 'active' : ''}>LINK2</a>
</Link>
</nav>
);
}
Here are the CSS Styles:
.navigation {
font-family: 'RobotoMono-Bold';
display: flex;
justify-content: center;
background-color: var(--clr-bg-secondary);
margin-bottom: 50px;
a {
color: #fff;
padding: 10px 22px;
}
a:hover {
background-color: #323952;
}
a:active {
background-color: #323952;
}
}
I would like that the background color attached to a:active will stay when the link is active. Probably something is wrong with react router??
Thanks for help!