加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ReactJS – 标记上的未知道具`activeClassName`.从元素中删除此p

发布时间:2020-12-15 20:51:13 所属栏目:百科 来源:网络整理
导读:我正在使用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
我正在使用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')
);

当我浏览/关于,我收到此错误:

“Warning: Unknown prop activeClassName on tag. Remove this prop from the element.

我在这做错了什么?

谢谢!

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>
)

记得从react-router-dom导入NavLink:

import {  NavLink } from 'react-router-dom'

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读