Answers for "nextjs active link"

0

next js active link

import Link from "next/link";
import { useRouter } from "next/router";


export const MyNav = () => {

  const router = useRouter();

  return (
    <ul>
      <li className={router.pathname == "/" ? "active" : ""}>
        <Link href="/">home</Link>
      </li>
      <li className={router.pathname == "/about" ? "active" : ""}>
        <Link href="/about">about</Link>
      </li>
    </ul>
  );
};
Posted by: Guest on June-03-2020
0

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;
Posted by: Guest on April-08-2021

Code answers related to "TypeScript"

Browse Popular Code Answers by Language