target active link when route is active nextjs code example
Example: nextjs active link
import Link from 'next/link';
import { useRouter } from 'next/router';
import React from 'react';
const NavLink = ({ href, children }) => {
const router = useRouter();
let className = children.props.className || '';
if (router.pathname === href) {
className = `${className} selected`;
}
return <Link href={href}>{React.cloneElement(children, { className })}</Link>
}
export default NavLink;