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:

  1. const isLinkActive = (match, location) => {
  2. return match
  3. };
  4.  
  5. const Nav = () => (
  6. <nav>
  7. <NavLink to="/" exact activeStyle={{color: 'pink'}}>Home</NavLink>
  8. <NavLink to="/about" activeClassName="active">About</NavLink>
  9. <NavLink replace
  10. to={{pathname: '/contact'}}
  11. isActive={isLinkActive}
  12. activeClassName="active"
  13. >Contact</NavLink>
  14. </nav>
  15. );

'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. 可执行EXE在windows调用过程

    举例图中, 一个C#编写的测试程序, 输出两句话分别 : Hello, GoodBye, 介绍其在windows上CLR的调用过程. 1.在执行Main方法之前, CLR会检测出Main的代码引用的所 ...

  2. 洛谷 P1287 盒子与球

    P1287 盒子与球 题目描述 现有r个互不相同的盒子和n个互不相同的球,要将这n个球放入r个盒子中,且不允许有空盒子.问有多少种方法? 例如:有2个不同的盒子(分别编为1号和2号)和3个不同的球(分 ...

  3. 第三次作业 201731082208 黄亚恒&肖莉

    Github项目地址:https://github.com/HYHSTUDEY/WordCount.git 作业地址:https://www.cnblogs.com/hyhhyh090628/p/10 ...

  4. JavaScript篇(一)二叉树的插入 (附:可视化)

    一.二叉树概念 二叉树(binary tree)是一颗树,其中每个节点都不能有多于两个的儿子. 字节一面,第一道就是二叉树的插入,在这里其实是对于一个二叉查找树的插入. 使二叉树成为二叉查找树的性质是 ...

  5. CSS外边距合并(塌陷/margin越界)

    原文 简书原文:https://www.jianshu.com/p/5f18f12cd162 大纲 1.什么是外边距合并?(折叠外边距) 2.外边距带来的影响 3.折叠的结果 4.产生折叠的原因 5. ...

  6. JVM route

    http://www.linuxidc.com/Linux/2013-06/86446.htm

  7. P2P借款的几种情况

    借款,至少出现2种人,借款人和出借人.根据人的性质,企业和个人,分成4种情况. 企业-个人,企业-企业,个人-企业,个人-个人. P2P平台可能出现几种情况: 个人-个人 2种情况:   a. 借款人 ...

  8. POJ 2752 Seek the Name, Seek the Fame (KMP)

    传送门 http://poj.org/problem?id=2752 题目大意:求既是前缀又是后缀的前缀的可能的长度.. 同样是KMP,和 HDU 2594 Simpsons' Hidden Tale ...

  9. PHP实现查询两个数组中不同元素的方法

    以下实例讲述了PHP实现查询两个数组中不同元素的方法.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...

  10. PHP的工作原理和生命周期

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u013778883/article/details/79831035   php是一门适用于web开 ...