简体   繁体   中英

How to keep current tabIndex when navigating between NextJS pages

How to properly save current tabIndex when navigating between pages?

(In ideas to make a function that will save the current index in the sessionStorage and retrieve it when the page is mounted)

Example:

在此处输入图片说明

在此处输入图片说明

a Simple solution based on the useRouter hook:

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="/">API</Link>
      </li>
      <li className={router.pathname == "/users" ? "active" : ""}>
        <Link href="/users">Users</Link>
      </li>
    </ul>
  );
};

You can use withRouter() library to figure out current URL and match it with the page URL and then add a different class with border or background-color to differentiate between them


You can also go with a much easier method and use :active and :visited selectors of CSS with the nav-item class or element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM