React-router: Got an error using activeClassName

Created on 4 Apr 2017  路  4Comments  路  Source: ReactTraining/react-router

Hi I'm using the sidebar example and want to mark the active page in the sidebar.

<li><NavItem to="/" className="fa fa-bars fa-2x" activeClassName="activeSidebar" aria-hidden="true"></NavItem></li>

But I got this error:

Warning: Unknown prop `activeClassName` on <a> tag. Remove this prop from the element. For details, see https://fb.me/react-unknown-prop
    in a (created by Link)
    in Link (at index.js:76)
    in li (at index.js:76)
    in ul (at index.js:75)
    in div (at index.js:74)
    in div (at index.js:73)
    in div (at index.js:72)
    in Router (created by BrowserRouter)
    in BrowserRouter (at index.js:71)

Whats the problem in this case?

Most helpful comment

Like @selbekk said, you need to use <NavLink>, not <Link>.

All 4 comments

Your example specifies <NavItem />, but the class is exposed as <NavLink /> - see if this helps you :)

NavLink is not known ...

same Problem with his:

<Link to="/" className="fa fa-bars fa-2x" activeClassName="activeSidebar"
                                  aria-hidden="true"></Link>

Like @selbekk said, you need to use <NavLink>, not <Link>.

react will warn you whenever you add an unknown property to a DOM-element, since it's most likely been put there by mistake. That's the case in your example, activeClassName is sent to Link which is sent to a, which prints the warning.

You have to import NavLink in order to use it - like this:

import { NavLink } from 'react-router-dom';
Was this page helpful?
0 / 5 - 0 ratings