Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navlink from react-router-dom Home always active

Nav works, home link is always active other links are OK. Adding as component, no props.

HTML

 <Menu/>

CSS

.active{
background-color:#ff6a00;
}

JS

import React, { Component } from 'react';
import './menu.css';
import { NavLink } from 'react-router-dom'
export default class TopMenu extends Component {

 render() {
    return (
        <div className="ui container">
            <div className="ui stackable menu">
                <div className="item">
                    <NavLink to='/' >
                        <i aria-hidden="true" className="home  icon" ></i>
                        Home
                     </NavLink>
                </div>
                <div className="item">
                    <NavLink to='/about' >
                        <i aria-hidden="true" className="circle info  icon" > 
                        </i>
                        About
                    </NavLink>
                </div>

                <div className="item" >
                    <NavLink to='/Settings'>
                        <i aria-hidden="true" className="cogs icon red" ></i>
                        Settings
                    </NavLink>
                </div>
            </div>
        </div>
     );
   }
 }

Ideas anyone, why is home always active ?

like image 529
fuzzybear Avatar asked Jul 06 '18 16:07

fuzzybear


People also ask

What is difference between Link and NavLink in dom router?

The NavLink is used when you want to highlight a link as active. So, on every routing to a page, the link is highlighted according to the activeClassName . Link is for links that need no highlighting. And a is for external links.


1 Answers

you have to specify the exact prop for your home route '/' because this matches all the other routes, that's why '/' is always active.

 <NavLink to='/' exact={true}>
   <i aria-hidden="true" className="home  icon" ></i>
   Home
 </NavLink>
like image 196
AngelSalazar Avatar answered Oct 11 '22 10:10

AngelSalazar