1

How can we change parent li className to active on the clicked route, any help would be appreciated

Problem is that li is the parent of ROUTER->LINK

import React, {Component} from 'react';
import {
  BrowserRouter as Router,
  Link,
  Route,
  Switch,
} from 'react-router-dom';

class Header extends Component {
    render() {
        return(
            <header className="nk-header nk-header-opaque">
                <nav className="nk-navbar nk-navbar-top nk-navbar-sticky nk-navbar-transparent nk-navbar-autohide">
                    <div className="container">
                        <div className="nk-nav-table">

                            <a href="index.html" className="nk-nav-logo">
                                <img src="assets/images/logo.png" alt="" width="90" />
                            </a>


                            <ul className="nk-nav nk-nav-right hidden-md-down" data-nav-mobile="#nk-nav-mobile">
                                <li className="active">
                                     <Link to="/" activeClassName="active">Home</Link>
                                </li>
                                <li className="  ">
                                     <Link to="/Product" activeClassName="active">Product</Link>
                                </li>
                            </ul>
                        </div>
                    </div>
                </nav>
            </header>     
        );
    }
}

export default Header;
Hardik Modha
  • 12,098
  • 3
  • 36
  • 40
Raj Jaril
  • 376
  • 1
  • 16
  • @ Raj please refer this : https://stackoverflow.com/questions/34418254/how-do-i-add-an-active-class-to-a-link-from-react-router – Archna Rangrej Aug 01 '18 at 07:12
  • change that will add styling attributes to the rendered element when it matches the current URL. – max li Aug 01 '18 at 07:23
  • @maxli thanks for reply, but i have to add class on li not to link – Raj Jaril Aug 01 '18 at 07:34
  • 1
    create a function const activeClass = (route) => { return if (this.props.location.pathname === route) "active" : null } use on
  • – max li Aug 01 '18 at 07:41
  • @max thanks its working – Raj Jaril Aug 01 '18 at 08:40