什么时候应该通过 NavLink 使用 Link? [英] When should I use Link over NavLink?
问题描述
参考 React Router v4 NavLink
是 Link
的一个变体,它允许你在它是活动链接时应用样式属性.这些样式属性似乎不是强制性的,我还没有找到任何描述使用 Link
的优点.
With reference to React Router v4 <NavLink> vs <Link> benefits and the official documentation, NavLink
is a variant of Link
that allows you to apply styling attributes when it is the active link. These styling attributes do not appear to be mandatory, and I haven't found anything describing the pros of using Link
.
如果是这样,为什么不到处使用 NavLink
呢?是否有任何理由选择 Link
而不是 NavLink
?也许是功能,还是更好的性能?
If that's the case, why not just use NavLink
everywhere? Are there any reasons to choosing Link
over NavLink
? Functionality perhaps, or better performance?
推荐答案
实际上,这两者之间的主要区别在于类属性.当我们使用 NavLink 作为标签时,它会在点击时自动继承一个活动类.另一方面,Link 标签现在在点击时有一个活动类.
我应该什么时候使用 NavLink?
正如名称所暗示的NavLink",我们主要在导航栏上使用它.这是因为活动类允许我们在 App.css 样式表中定义我们的自定义样式.因此,我们可以使用它来设计我们的活动按钮的样式,以通知他/她当前在哪个页面上的使用.
我应该什么时候使用链接?
Link 标签可以用在我们只想做一些没有特殊效果的路由的地方.例如;我们可以将 Link 标签用于滚动到顶部按钮、添加到卡片按钮、提交按钮等.
这篇关于什么时候应该通过 NavLink 使用 Link?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!