React Router V6 ISACTIVE不更新样式

发布于 2025-02-07 03:05:34 字数 643 浏览 0 评论 0原文

升级到React Route Router V6后,有一个问题,即在活动活动时更新navlink的样式。

代码看起来像这样:

{siteLinks.map({ href, name }) => {
  return (
    <NavLink
      key={href}
      to={href}
      style={({ isActive }) => ({
        backgroundColor: isActive ? 'gray' : 'transparent',
     })}
    >
       <p>{name}</p>
   </NavLink>
}

当我检查元素时,我会看到a标签具有active> active className,但style未被选中向上。

如果我更改以下内容,它们都显示为灰色

<NavLink
   key={href}
   to={href}
   style={{ backgroundColor: 'gray' }}
>

After upgrading to react router v6 there is an issue updating the style of a NavLink when it is active.

The code looks like this:

{siteLinks.map({ href, name }) => {
  return (
    <NavLink
      key={href}
      to={href}
      style={({ isActive }) => ({
        backgroundColor: isActive ? 'gray' : 'transparent',
     })}
    >
       <p>{name}</p>
   </NavLink>
}

When I inspect the elements I see that the a tag has the active classname but the style doesn't get picked up.

If I change to the following they all show as gray

<NavLink
   key={href}
   to={href}
   style={{ backgroundColor: 'gray' }}
>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最偏执的依靠 2025-02-14 03:05:34

尝试使用className

className={({ isActive }) =>
                      isActive  
                        ? someClass
                        : someOtherClass
                    }

Give it a try with a className

className={({ isActive }) =>
                      isActive  
                        ? someClass
                        : someOtherClass
                    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文