React Navlink:ISACTIVE函数thrws' String无法分配到类型字符串|未定义'
我定义了用于造型活动链接的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
ActiveClassName
。您可以简单地执行此操作:但是它在React-Router-Dom V5中起作用。它不再存在于V6中。有关更多详细信息请参阅文档
Use
activeClassName
instead. You can simply do this:But it works in react-router-dom v5. It does not exist in v6 anymore. For further details see documentation
className
不接受回调功能。如果ISACTIVE
一个变量或状态,则可以执行此操作:className
does not accept callback functions. IfisActive
a variable or a state you can simply do this: