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.

[React Router v4] Style a Link that is Active with NavLink的更多相关文章

  1. [Web 前端] React Router v4 入坑指南

    cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...

  2. React Router V4发布

    React Router V4 正式版发布,该版本相较于前面三个版本有根本性变化,遵循 Just Component 的 API 设计理念. 本次升级的主要变更有: 声明式 Declarative 可 ...

  3. [React Router v4] Intercept Route Changes

    If a user has entered some input, or the current Route is in a “dirty” state and we want to confirm ...

  4. [React Router v4] Redirect to Another Page

    Overriding a browser's current location without breaking the back button or causing an infinite redi ...

  5. [React Router v4] Render Multiple Components for the Same Route

    React Router v4 allows us to render Routes as components wherever we like in our components. This ca ...

  6. [React Router v4] Conditionally Render a Route with the Switch Component

    We often want to render a Route conditionally within our application. In React Router v4, the Route ...

  7. [React Router v4] Render Catch-All Routes with the Switch Component

    There are many cases where we will need a catch-all route in our web applications. This can include ...

  8. [React Router v4] Render Nested Routes

    With React Router v4 the entire library is built as a series of React components. That means that cr ...

  9. [React Router v4] Parse Query Parameters

    React Router v4 ignores query parameters entirely. That means that it is up to you to parse them so ...

随机推荐

  1. 洛谷 P2384 最短路

    洛谷 P2384 最短路 题目背景 狗哥做烂了最短路,突然机智的考了Bosh一道,没想到把Bosh考住了...你能帮Bosh解决吗? 他会给你10000000000000000000000000000 ...

  2. event事件对象 兼容写法:var oEvent=ev||event;和取消事件冒泡

    要在整个页面添加事件要使用document,例如要捕抓鼠标位置 document.onclink=function(ev)  //FireFox Chrome默认都是有一个值传进来的 { var oE ...

  3. 笔记二:JS的输出、语法、语句、字符串、条件语句、switch语句、for循环、while循环

    1.JS的输出: 注意:JS没有任何打印或者输出的函数 JS输出数据的集中方法:  1.使用window.alert()弹出警告框: 2.使用document.write()方法将内容写到HTML文档 ...

  4. Sql Server 2014完全卸载

    经历过好多次Sql server的安装与卸载,有时发现自己卸载的费时费力,单纯地卸载个软件就要吐血了,那么现在我觉得是时候整理一下了. 1.在运行中输入services.msc,然后找到所有跟Sql ...

  5. EventWaitHandle

    在查资料的过程中,我突然想到一个类:EventWaitHandle,也就是本文的主角. 这个类通过在线程之间设置信号量,可以非常方便的控制线程运行的顺序.具体代码如下: 首先全局申明: EventWa ...

  6. Redo current损坏

       如果损坏的是current redo log (select group#,sequence#,archived,status from v$log;) 有两种情况: A.   数据库是正常关闭 ...

  7. FZU Problem 2062 Suneast & Yayamao

    http://acm.fzu.edu.cn/problem.php?pid=2062 题目大意: 给你一个数n,要求求出用多少个数字可以表示1~n的所有数. 思路: 分解为二进制. 对于一个数n,看它 ...

  8. ZOJ 1242 Carbon Dating

    UVA昨天上不去,今天一大早起来还是上不去 0.0 于是去ZOJ 这题大意就是半衰期... 取对数用到了换底公式...我都忘了这玩意了T T 上代码... #include<iostream&g ...

  9. UML学习总结(3)——StarUML指导手册

    StarUML使用说明-指导手册 原著:Stephen Wong            翻译:火猴 StarUML是一种生成类图和其他类型的统一建模语言(UML)图表的工具.这是一个用Java语言描述 ...

  10. Java 学习(18):Java 序列化& 网络编程& 发送邮件

    --Java 序列化 -- 网络编程 -- 发送邮件 Java 序列化 Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信 ...