S
S
Smuzzzzi2018-11-04 23:55:26
React
Smuzzzzi, 2018-11-04 23:55:26

How to assign className="active" to li?

import {NavLink} from 'react-router-dom';
<li className="active">
  <NavLink to="/">
    <u>Главная</u>
  </NavLink>
</li>
<li className="active">
  <NavLink to="/about">
    <u>О нас</u>
  </NavLink>
</li>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nakree, 2018-11-05
@Smuzzzzi

NavLink

import {NavLink} from 'react-router-dom';
<li>
  <NavLink to="/" activeClassName="active">
    <u>Главная</u>
  </NavLink>
</li>
<li>
  <NavLink to="/about" activeClassName="active">
    <u>О нас</u>
  </NavLink>
</li>

A
Anton Spirin, 2018-11-05
@rockon404

import { Link, withRouter } from 'react-router-dom';

const Example = ({ location }) => (
  <ul>
    <li className={location.pathname === '/' ? 'active' : ''}>
      <Link to="/">
        <u>Главная</u>
      </Link>
    </li>
    <li className={location.pathname === '/about' ? 'active' : ''}>
      <Link to="/about">
        <u>О нас</u>
      </Link>
    </li>
  </ul>
);

export default withRouter(Example);

Demo.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question