[React Router v4] Style a Link that is Active with NavLink
2024-08-31 18:45:17
We often need to be able to apply style to navigation links based on the current route. In React Router v4 you can easily accomplish this with the NavLink component. In this lesson, we will step through three ways to accomplish styling links through the use of an active class with the NavLink component.
There are three ways to check an link is active or not and add active class for it:
const isLinkActive = (match, location) => {
return match
}; const Nav = () => (
<nav>
<NavLink to="/" exact activeStyle={{color: 'pink'}}>Home</NavLink>
<NavLink to="/about" activeClassName="active">About</NavLink>
<NavLink replace
to={{pathname: '/contact'}}
isActive={isLinkActive}
activeClassName="active"
>Contact</NavLink>
</nav>
);
'isActive' prop allows you to decide whether or not to apply active class.
最新文章
- 如何实现一个php框架系列文章【4】url路由管理
- Jmeter+TCP\Scoket(8583)报文压力测试
- shadow Dom(shadowRoot) 访问
- 如何更好的通过Inflate layout的方式来实现自定义view
- VS2010 自动跳过代码现象
- 获得 LayoutInflater 实例的三种方式
- javascript 我是广告
- log4cxx入门第一篇--一个小例子
- Python学习--20 Web开发
- 老李分享:Robotium创建Note的实例
- 英语学习笔记---01---新概念第一册---Lesson 1 Excuse me!
- PGM:图模型学习概述
- LCD 常用的客观效果指标和测试方法
- SpringMVC解决跨域的两种方案
- 部署alinode监控线上应用
- python中os模块操作目录与文件名小结
- Maven遇到github引用的项目有bug怎么办?
- spring cloud: 使用consul来替换eureka
- lambda表达式底层处理机制
- [转]Windows7:Visual Studio 2008试用版的评估期已经结束解决方法