如何在react-router中为Link或IndexLink的包装元素设置activeClassName?

abe*_*nza 47 javascript reactjs react-router redux

我是ReactJS世界的新手,想知道如何将活动类名传递给<li>元素而不是<a>(Link)元素.

现在我有这种代码.单击时锚类会更改.

<li><IndexLink to='/' activeclassName='active'>A</IndexLink></li>
<li><Link to='/b' activeclassName='active'>B</Link></li>
<li><Link to='/c' activeclassName='active'>C</Link></li>
Run Code Online (Sandbox Code Playgroud)

但我想得到类似的东西:

<li activeclassName='active'><IndexLink to='/'>A</IndexLink></li>
<li activeclassName='active'><Link to='/b'>B</Link></li>
<li activeclassName='active'><Link to='/c'>C</Link></li>
Run Code Online (Sandbox Code Playgroud)

提前致谢

Mar*_*ock 37

您需要将您<li>作为路由器识别组件包含在内:

import { Link, IndexLink } from 'react-router'

class NavItem extends React.Component {
  render () {
    const { router } = this.context
    const { index, onlyActiveOnIndex, to, children, ...props } = this.props

    const isActive = router.isActive(to, onlyActiveOnIndex)
    const LinkComponent = index ? Link : IndexLink

    return (
      <li className={isActive ? 'active' : ''}>
        <LinkComponent {...props}>{children}</LinkComponent>
      </li>
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

用法:

<ul>
  <NavItem to='/' index={true}>Home</NavItem>
  <NavItem to='/a'>A</NavItem>
</ul>
Run Code Online (Sandbox Code Playgroud)

我从react-router-bootstrap模块中获取了灵感,https://github.com/react-bootstrap/react-router-bootstrap/blob/master/src/LinkContainer.js.我没有测试它,所以让我知道它是怎么回事.

  • 根据您的设置,您可能需要`NavLi.contextTypes = {router:React.PropTypes.object}; 导出默认NavLi;` (3认同)

mpe*_*pen 18

其他答案在React Router v4中似乎不起作用.这是你如何做到的:

import React, {PropTypes} from 'react'
import {Route, Link} from 'react-router-dom'
import styles from './styles.less';

export default function NavItem({children, to, exact}) {
    return (
        <Route path={to} exact={exact} children={({match}) => (
            <li className={match ? styles.activeRoute : null}>
                <Link to={to}>{children}</Link>
            </li>
        )}/>
    )
}

NavItem.propTypes = {
    to: PropTypes.string.isRequired,
    exact: PropTypes.bool,
    children: PropTypes.node.isRequired,
};
Run Code Online (Sandbox Code Playgroud)

  • 这次真是万分感谢.我会将``props`添加到params并在`<Link to = {to} {... props}> {children} </ Link>`中使用它以获得更灵活. (2认同)

bry*_*nyu 10

/**
 * 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
Run Code Online (Sandbox Code Playgroud)

用法:

<ul className="nav nav-tabs"> 
  <NavItem to='/home' index={true} >Home</NavItem>
  <NavItem to='/about'>About</NavItem>
</ul>
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用,但由于我使用的是IndexLink,我做了一次更改:else isActive = router.isActive(to,true).我必须在IsActive函数中添加true,否则isActive总是为'/'返回true. (2认同)

ken*_*ley 5

{/* Make sure that `location` is injected into this component */}
<ul className="nav navbar-nav">
  <li className={location.pathname === '/' && 'active'}>
    <Link to='/'>
      Home page
    </Link>
  </li>
  <li className={location.pathname.startsWith('/about') && 'active'}>
    <Link to='/about'>
      About us
    </Link>
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)


小智 5

而不是使用<Link />,我使用<NavLink />,它也有效.

import React, { Component } from 'react';
import { NavLink } from 'react-router-dom';

//.....

export default class AppNav extends Component {

    render (){
        return (
                <header>
                    <ul className="main-nav">
                        <li><NavLink activeClassName={"active"} exact={true} to="/">Home</NavLink></li>
                        <li><NavLink activeClassName={"active"} to="/about">About</NavLink></li>
                        <li><NavLink activeClassName={"active"} to="/courses">Courses</NavLink></li>
                    </ul>
                </header>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这不是问题。他问父母班。 (3认同)
  • 它是否设置了父`&lt;li&gt;` 元素的类?对我来说,它只是设置从 `&lt;NavLink&gt;` 创建的 `&lt;a&gt;` 的类。 (2认同)