Active link with React-Router?

前端 未结 1 1741
感动是毒
感动是毒 2020-11-30 02:10

I\'m trying out React-Router (v4) and I\'m having issues starting off the Nav to have one of the Link\'s be active. If I click on any of the

相关标签:
1条回答
  • 2020-11-30 02:39

    <Link> no longer has the activeClassName or activeStyle properties. In react-router v4 you have to use <NavLink> if you want to do conditional styling:

    const Router = () => (
      <BrowserRouter>
        <div>
          <Nav>
            <NavLink exact={true} activeClassName='is-active' to='/'>Home</NavLink>
            <NavLink activeClassName='is-active' to='/about'>About</NavLink>
          </Nav>
    
          <Match pattern='/' exactly component={Home} />
          <Match pattern='/about' exactly component={About} />
          <Miss component={NoMatch} />
        </div>
      </BrowserRouter>
    )
    

    I added an exact property to the home <NavLink>, I'm fairly sure that without it, the home link would always be active since / would match /about and any other pages you have.

    0 讨论(0)
提交回复
热议问题