How to set activeClassName for wrapper element of Link or IndexLink in react-router?

前端 未结 10 1489
醉酒成梦
醉酒成梦 2020-12-07 15:56

I am new to the ReactJS world, and would like to know how can I pass active class name to the

  • element instead of (Link) element
  • 相关标签:
    10条回答
    • 2020-12-07 16:29

      Great answer.

      Just change the following to get it working...

      LinkComponent = index ? IndexLink : Link //If its true you want an IndexLink 
      
      //Also link needs to be...
      <NavItem to="/" onlyActiveOnIndex index={true}>Home</NavItem>
      
      0 讨论(0)
    • 2020-12-07 16:31

      Try with react-router-active-component.

      I couldn't get any of the previous answers to easily work due to incompatibilities between react or typescript versions (this is definitely not a mature ecosystem), but this component did the trick, and can be applied to other elements than li if needed:

      import activeComponent from 'react-router-active-component'
      let NavItem = activeComponent('li');
      ...
      <NavItem to='/' onlyActiveOnIndex>Home</NavItem>
      <NavItem to='/generate-keywords'>Generate keywords</NavItem>
      
      0 讨论(0)
    • 2020-12-07 16:34
      /**
       * A navigation component
       */
      import React, { Component } from 'react'
      import { Link, IndexLink, withRouter } from 'react-router'
      
      import styles from './styles.scss'
      
      class NavItem extends Component {
        render () {
          const { router } = this.props
          const { index, to, children, ...props } = this.props
      
          let isActive
          if( router.isActive('/',true) && index ) isActive = true
          else  isActive = router.isActive(to)
          const LinkComponent = index ?  IndexLink : Link
      
          return (
            <li className={isActive ? 'active' : ''}>
              <LinkComponent to={to} {...props}>{children}</LinkComponent>
            </li>
          )
        }
      }
      
      NavItem = withRouter(NavItem)
      
      export default NavItem
      

      Usage:

      <ul className="nav nav-tabs"> 
        <NavItem to='/home' index={true} >Home</NavItem>
        <NavItem to='/about'>About</NavItem>
      </ul>
      
      0 讨论(0)
    • 2020-12-07 16:34

      Using React Router v4, I only got it to work by including the <li> tags within the NavLink component. The solutions which have the <li> tags wrapping the links resulted in the HOME <li> tag always having the active class.

      import React from 'react'
      import { NavLink } from 'react-router-dom';
      
      class Header extends React.Component {
      
        render() {
          return (
            <div>
              <header>
      
                <nav>
                  <ul>
                    <NavLink activeClassName={"active"} exact={true} to="/"><li>Home</li></NavLink>
                    <NavLink activeClassName={"active"} to="/about"><li>About</li></NavLink>
                    <NavLink activeClassName={"active"} to="/courses"><li>Courses</li></NavLink>
                  </ul>
                </nav>
      
              </header>
            </div>
          )
        }
      }
      
      export default Header
      

      I adjusted the li and a CSS selectors accordingly.

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