ReactJS - <a>标签上的未知道具`activeClassName`.从元素中删除此prop

hab*_*bun 9 reactjs react-router

我正在使用react 15.4.2和react-router4.0.0,这个项目是用Create React App引导的.

这是我的代码.

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import {BrowserRouter as Router,Route,Link} from 'react-router-dom'


 const AboutPage = () => {
 return(
    <section>
        <h2>This is About page</h2>
        <Link activeClassName="active" to="/about/nestedone">Nestedone</Link>
        {' '}
        <Link activeClassName="active" to="/about/nestedtwo">Nested two</Link>
    </section>
)
}

const HomePage = () => {
return(
    <section>
        <h2>This is Home page</h2>
        <Link to="/about">About</Link>
    </section>
)
}

const NestedOne = () => {
return (
    <section>
        <h2>Nested page 1</h2>
    </section>
)
}


const NestedTwo = () => {
return (
    <section>
        <h2>Nested page 2</h2>
    </section>
)
}


 ReactDOM.render(
 <Router> 
  <section>
    <Route exact path="/" component={HomePage} />
    <Route path="/about" component={AboutPage} />
    <Route path="/about/nestedone" component={NestedOne} />
    <Route path="/about/nestedtwo" component={NestedTwo} />
 </section>
 </Router>,
  document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)

当我浏览时/about,我收到此错误:

"警告:activeClassName标签上的未知支柱.从元素中删除此支柱.

我在这做错了什么?

谢谢!

小智 40

从React-router v6开始,替换activeClassNameclassName并使用navData.isActive来切换样式。


老办法:

<NavLink to="/home" activeClassName="active-style">Home</NavLink>
Run Code Online (Sandbox Code Playgroud)

v6 及以上版本:

<NavLink to="/home" className={(navData) => (navData.isActive ? "active-style" : 'none')}>Home</NavLink>
Run Code Online (Sandbox Code Playgroud)

或者你也可以isActive从以下结构解构navData

 <NavLink to="/home" className={({isActive}) => (isActive ? "active-style" : 'none')}>Home</NavLink>
Run Code Online (Sandbox Code Playgroud)


cod*_*ade 16

activeClassName物业不是Link的属性,而是NavLink的属性.

因此,只需将代码更改为使用NavLink而不是Link:

const AboutPage = () => {
 return(
    <section>
        <h2>This is About page</h2>
        <NavLink activeClassName="active" to="/about/nestedone">Nestedone</NavLink>
        {' '}
        <NavLink activeClassName="active" to="/about/nestedtwo">Nested two</NavLink>
    </section>
)
Run Code Online (Sandbox Code Playgroud)

请记住NavLink从以下位置导入react-router-dom:

import {  NavLink } from 'react-router-dom'
Run Code Online (Sandbox Code Playgroud)

  • 即使使用 NavLink,我也会收到此错误......不知道为什么会抛出它。 (2认同)

小智 12

在 React Router v6 中,activeClassName已更改为仅可以使用classNamepropisActive来操纵样式。

欲了解更多信息
https://reactrouter.com/docs/en/v6/upgrading/v5#remove-activeclassname-and-activestyle-props-from-navlink-

const AboutPage = () => {
    return(
        <section>
        <h2>This is About page</h2>
        <NavLink className={({ isActive }) => isActive? "active": ''} to="/about/nestedone">Nestedone</NavLink>
        {' '}
        <NavLink className={({ isActive }) => isActive? "active": ''} to="/about/nestedtwo">Nested two</NavLink>
        </section>
    )
}
Run Code Online (Sandbox Code Playgroud)


Dam*_*oux 6

activeClassName不是属性LinkNavLink.

由于react-router v4 beta8,该属性active默认为.验证节点模块文件夹中安装的版本