简体   繁体   English

在 NextJS 页面之间导航时如何保持当前 tabIndex

[英]How to keep current tabIndex when navigating between NextJS pages

How to properly save current tabIndex when navigating between pages?在页面之间导航时如何正确保存当前 tabIndex?

(In ideas to make a function that will save the current index in the sessionStorage and retrieve it when the page is mounted) (想法是制作一个函数,将当前索引保存在 sessionStorage 中并在加载页面时检索它)

Example:例子:

在此处输入图片说明

在此处输入图片说明

a Simple solution based on the useRouter hook:基于useRouter钩子的简单解决方案:

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您可以使用withRouter()库找出当前 URL 并将其与页面 URL 进行匹配,然后添加带有borderbackground-color的不同类来区分它们


You can also go with a much easier method and use :active and :visited selectors of CSS with the nav-item class or element.您还可以使用更简单的方法,将 CSS 的:active:visited选择器与nav-item类或元素一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC如何在页面之间导航时保​​持搜索值? - MVC How to keep search value when navigating between pages? 如何仅按正的tabindex导航 - How to navigating only by positive tabindex 导航到新页面时,如何使切换开关保持在当前位置? - How can I make my toggle switches stay in their current positions when navigating to new pages? Firebase 导航到其他页面时保持用户登录 - Firebase keep user logged in when navigating to other pages 浏览页面时如何保持音频播放? - How to keep audio playing while navigating through pages? 在页面之间导航时,导航栏和链接会闪烁/跳转/调整大小,如何解决? - Navigation bar and links flicker/jump/resize when navigating between pages, how to fix? [Python] [Tornado]:在页面之间导航时出现500内部服务器错误 - [Python][Tornado]: 500 Internal Server Error when navigating between pages 在移动设备上的页面之间导航时保​​持屏幕分辨率 - Maintaining screen resolution when navigating between pages on mobile device 如何使用表单在页面之间保留会话变量? - How to keep session variables between pages with forms? 导航到其他模式时如何保持模式的输入值? - How to keep input value from a modal when navigating to other modal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM