React 路由器中的 activeClassName 同时突出显示两个 NavLink
activeClassName in react router is highlighting two NavLinks at the same time
我正在学习 React Router 并关注 this tutorial
除了突出显示活动 NavLink 的问题外,一切都清楚并按照说明工作(感谢作者)。
查看 css 文件并记下 .current class:
nav ul {
list-style: none;
display: flex;
background-color: black;
margin-bottom: 20px;
}
nav ul li {
padding: 20px;
}
nav ul li a {
color: white;
text-decoration: none;
}
.current {
border-bottom: 4px solid white;
}
当我加载应用程序时,导航中的主页 link 以白色底部边框线突出显示,但即使我单击 "about" link 或"Contact" link。我一步一步地跟着一切,但我无法弄清楚这个问题。
在阅读了关于 NavLink 的 React 文档和许多在线教程之后,仍然不知道如何修复它。
这是我点击关于 link
这是我点击“联系”时的样子 link
这就是教程路由示例的方式
<nav>
<ul>
<li><NavLink exact activeClassName="current" to='/'>Home</NavLink></li>
<li><NavLink exact activeClassName="current" to='/about'>About</NavLink></li>
<li><NavLink exact activeClassName="current" to='/contact'>Contact</NavLink></li>
</ul>
</nav>
注意主页 link 如何保持其下方的活动白线,尽管事实上它不再是活动 link。
添加 activeClassName="current" 假设用白色下划线突出显示当前视图,但主页 link 仍然用白色下划线突出显示。
所以,请所有有多年经验的人帮忙。
这是我的 app.js
import React, { Component } from 'react';
import './App.css';
import Main from './components/main';
import Navigation from './components/navigation';
class App extends Component {
render() {
return (
<div>
<h1> React Router </h1>;
<h5> import 'BrowserRouter' from 'react-router-dom' in Appjs</h5>
<hr></hr>
<Navigation />
<Main />
</div>
);
}
}
export default App;
这是我的 index.js
import React from 'react';
import ReactDOM from 'react-dom';
//Add browserrouter to use it in your app
import { BrowserRouter } from 'react-router-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
//this router example is from this site
//https://blog.pusher.com/getting-started-with-react-router-v4/
/*
You can only pass a single child element to a Router,
so it is necessary to create a root component
that renders the rest of your application and
then pass that in as the child of the Router.
*/
ReactDOM.render((
<BrowserRouter>
<App />
</BrowserRouter>
), document.getElementById('root'));
这是我的 main.js
import React, { Component } from 'react';
import { NavLink, Switch, Route } from 'react-router-dom';
import Home from './home';
import About from './about'
import Contact from './contact'
class Main extends Component {
render() {
return (
<div>
<p> My main component </p>
<Switch>
<Route exact={true} path='/' component={Home}></Route>
<Route path='/about' component={About}></Route>
<Route path='/contact' component={Contact}></Route>
</Switch>
</div>
);
}
}
export default Main;
最后是我的 navigation.js
import React, { Component } from 'react';
import { NavLink } from 'react-router-dom';
class Navigation extends Component {
render() {
return (
<div>
<p> My Navigation component </p>
<nav>
<ul>
<li><NavLink to='/'> Home </NavLink> </li>
<li><NavLink to='/about'> About </NavLink> </li>
<li><NavLink to='/contact'> Contact </NavLink> </li>
</ul>
</nav>
</div>
);
}
}
export default Navigation;
你应该将 exact
属性 添加到你的 root("") 路由,否则,它将匹配所有以 "" 开头的路由,React Router exact
例如:
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />
更新:
改变顺序也有同样的效果,但不完全相同 属性
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />
<Route path="/" component={Home} />
在路由开关的一侧,它会转到第一个匹配的路由,如果我们在开头添加路径为 /
的路由,它将始终在路径中渲染组件,它最好避免使用 order
进行精确修复
Navlink
还需要一个精确的...更改为:
import React, { Component } from 'react';
import { NavLink } from 'react-router-dom';
class Navigation extends Component {
render() {
return (
<div>
<p> My Navigation component </p>
<nav>
<ul>
<li><NavLink to='/' activeClassName="current" exact={true}> Home </NavLink> </li>
<li><NavLink to='/about' activeClassName="current"> About </NavLink> </li>
<li><NavLink to='/contact' activeClassName="current"> Contact </NavLink> </li>
</ul>
</nav>
</div>
);
}
}
export default Navigation;
必须向 NavLink 组件提供精确的属性。
它的工作原理是这样的,
家
图片
当用户点击主页时,url='/me',它加载主页组件,当用户点击图片时,url='/me/pictures',如果NavLink 中未提供 exact,则 url 都包含“/me”,这使它们都处于活动状态。
它是 NavLinks 精确道具的用例。
对于 v6,使用 end
而不是 exact
<NavLink end activeClassName="activeNavLink" to="/">
您可以根据需要在 Nav Links 中使用 exact 和 strict。
从 <NavLink />
中删除 activeClassName
和 activeStyle
道具
从 v6.0.0-beta.3 开始,activeClassName
和 activeStyle
道具已从 NavLinkProps
中移除。相反,您可以将函数传递给 style
或 className
,这将允许您根据组件的 active state
自定义 inline styling
或 class string
。
类名
<NavLink
to="/"
className={({ isActive }) => "nav-link" + (isActive ? " activated" : "")}
>
Home
</NavLink>
风格
<NavLink
to="/"
style={({ isActive }) => ({ color: isActive ? 'green' : 'blue' })}
>
Home
</NavLink>
我正在学习 React Router 并关注 this tutorial 除了突出显示活动 NavLink 的问题外,一切都清楚并按照说明工作(感谢作者)。
查看 css 文件并记下 .current class:
nav ul {
list-style: none;
display: flex;
background-color: black;
margin-bottom: 20px;
}
nav ul li {
padding: 20px;
}
nav ul li a {
color: white;
text-decoration: none;
}
.current {
border-bottom: 4px solid white;
}
当我加载应用程序时,导航中的主页 link 以白色底部边框线突出显示,但即使我单击 "about" link 或"Contact" link。我一步一步地跟着一切,但我无法弄清楚这个问题。
在阅读了关于 NavLink 的 React 文档和许多在线教程之后,仍然不知道如何修复它。
这是我点击关于 link
这是我点击“联系”时的样子 link
这就是教程路由示例的方式
<nav>
<ul>
<li><NavLink exact activeClassName="current" to='/'>Home</NavLink></li>
<li><NavLink exact activeClassName="current" to='/about'>About</NavLink></li>
<li><NavLink exact activeClassName="current" to='/contact'>Contact</NavLink></li>
</ul>
</nav>
注意主页 link 如何保持其下方的活动白线,尽管事实上它不再是活动 link。
添加 activeClassName="current" 假设用白色下划线突出显示当前视图,但主页 link 仍然用白色下划线突出显示。
所以,请所有有多年经验的人帮忙。
这是我的 app.js
import React, { Component } from 'react';
import './App.css';
import Main from './components/main';
import Navigation from './components/navigation';
class App extends Component {
render() {
return (
<div>
<h1> React Router </h1>;
<h5> import 'BrowserRouter' from 'react-router-dom' in Appjs</h5>
<hr></hr>
<Navigation />
<Main />
</div>
);
}
}
export default App;
这是我的 index.js
import React from 'react';
import ReactDOM from 'react-dom';
//Add browserrouter to use it in your app
import { BrowserRouter } from 'react-router-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
//this router example is from this site
//https://blog.pusher.com/getting-started-with-react-router-v4/
/*
You can only pass a single child element to a Router,
so it is necessary to create a root component
that renders the rest of your application and
then pass that in as the child of the Router.
*/
ReactDOM.render((
<BrowserRouter>
<App />
</BrowserRouter>
), document.getElementById('root'));
这是我的 main.js
import React, { Component } from 'react';
import { NavLink, Switch, Route } from 'react-router-dom';
import Home from './home';
import About from './about'
import Contact from './contact'
class Main extends Component {
render() {
return (
<div>
<p> My main component </p>
<Switch>
<Route exact={true} path='/' component={Home}></Route>
<Route path='/about' component={About}></Route>
<Route path='/contact' component={Contact}></Route>
</Switch>
</div>
);
}
}
export default Main;
最后是我的 navigation.js
import React, { Component } from 'react';
import { NavLink } from 'react-router-dom';
class Navigation extends Component {
render() {
return (
<div>
<p> My Navigation component </p>
<nav>
<ul>
<li><NavLink to='/'> Home </NavLink> </li>
<li><NavLink to='/about'> About </NavLink> </li>
<li><NavLink to='/contact'> Contact </NavLink> </li>
</ul>
</nav>
</div>
);
}
}
export default Navigation;
你应该将 exact
属性 添加到你的 root("") 路由,否则,它将匹配所有以 "" 开头的路由,React Router exact
例如:
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />
更新:
改变顺序也有同样的效果,但不完全相同 属性
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />
<Route path="/" component={Home} />
在路由开关的一侧,它会转到第一个匹配的路由,如果我们在开头添加路径为 /
的路由,它将始终在路径中渲染组件,它最好避免使用 order
Navlink
还需要一个精确的...更改为:
import React, { Component } from 'react';
import { NavLink } from 'react-router-dom';
class Navigation extends Component {
render() {
return (
<div>
<p> My Navigation component </p>
<nav>
<ul>
<li><NavLink to='/' activeClassName="current" exact={true}> Home </NavLink> </li>
<li><NavLink to='/about' activeClassName="current"> About </NavLink> </li>
<li><NavLink to='/contact' activeClassName="current"> Contact </NavLink> </li>
</ul>
</nav>
</div>
);
}
}
export default Navigation;
必须向 NavLink 组件提供精确的属性。
它的工作原理是这样的, 家 图片
当用户点击主页时,url='/me',它加载主页组件,当用户点击图片时,url='/me/pictures',如果NavLink 中未提供 exact,则 url 都包含“/me”,这使它们都处于活动状态。
它是 NavLinks 精确道具的用例。
对于 v6,使用 end
而不是 exact
<NavLink end activeClassName="activeNavLink" to="/">
您可以根据需要在 Nav Links 中使用 exact 和 strict。
从 <NavLink />
中删除 activeClassName
和 activeStyle
道具
从 v6.0.0-beta.3 开始,activeClassName
和 activeStyle
道具已从 NavLinkProps
中移除。相反,您可以将函数传递给 style
或 className
,这将允许您根据组件的 active state
自定义 inline styling
或 class string
。
类名
<NavLink
to="/"
className={({ isActive }) => "nav-link" + (isActive ? " activated" : "")}
>
Home
</NavLink>
风格
<NavLink
to="/"
style={({ isActive }) => ({ color: isActive ? 'green' : 'blue' })}
>
Home
</NavLink>