React Navlink:ISACTIVE函数thrws' String无法分配到类型字符串|未定义'

发布于 2025-02-08 18:15:54 字数 346 浏览 0 评论 0原文

我定义了用于造型活动链接的NAVLinks。

例如,我尝试了这种方法的几种方法:

<NavLink to="contact" className={({ isActive }) => (isActive ? 'nav-links-active' : 'nav-links')}>
      Contact
</NavLink>

这是不起作用的,IDE返回:

type函数({isActive:any}):string |字符串无法分配到类型字符串|未定义字符串不能分配到类型字符串,

我必须在某个地方定义ISARTIVE吗?我在做什么错?

I defined NavLinks for styling an active link.

For instance I tried several approaches of this kind:

<NavLink to="contact" className={({ isActive }) => (isActive ? 'nav-links-active' : 'nav-links')}>
      Contact
</NavLink>

This doesn't work and the IDE returns:

Type function({isActive: any}): string | string is not assignable to type string | undefined   Type function({isActive: any}): string | string is not assignable to type string

Do I have to define isActive somewhere? What am I doing wrong?

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

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

发布评论

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

评论(3

<逆流佳人身旁 2025-02-15 18:15:54

使用ActiveClassName。您可以简单地执行此操作:

<NavLink to="contact" activeClassName='nav-links-active' className='nav-links'>
      Contact
</NavLink>

但是它在React-Router-Dom V5中起作用。它不再存在于V6中。有关更多详细信息请参阅文档

Use activeClassName instead. You can simply do this:

<NavLink to="contact" activeClassName='nav-links-active' className='nav-links'>
      Contact
</NavLink>

But it works in react-router-dom v5. It does not exist in v6 anymore. For further details see documentation

稀香 2025-02-15 18:15:54

默认情况下,一个活动类是在组件中添加
是活跃的。这为大多数人提供了相同的简单造型机制
从V5升级的用户。 v6.0.0-beta.3的一个区别是
ActiveClassname和Activestyle已从
navlinkprops。相反,您可以将功能传递给样式或
ClassName将允许您自定义内联样式或
基于组件的活动状态的类字符串。你也可以通过
作为孩子的功能,可以自定义
组件基于其活跃状态,特别可更改
内部元素的样式。

By default, an active class is added to a component when it
is active. This provides the same simple styling mechanism for most
users who are upgrading from v5. One difference as of v6.0.0-beta.3 is
that activeClassName and activeStyle have been removed from
NavLinkProps. Instead, you can pass a function to either style or
className that will allow you to customize the inline styling or the
class string based on the component's active state. You can also pass
a function as children to customize the content of the
component based on their active state, specially useful to change
styles on internal elements.

墨小墨 2025-02-15 18:15:54

className不接受回调功能。如果ISACTIVE一个变量或状态,则可以执行此操作:

<NavLink to="contact" className={isActive ? 'nav-links-active' : 'nav-links'}>
      Contact
</NavLink>

className does not accept callback functions. If isActive a variable or a state you can simply do this:

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