React.js 实现菜单 [突出显示活动 link]
React.js implement menu [highlight active link]
以下 React.js 代码呈现一个导航栏,其中包含两个名为 'about' 和 'project' 的 link。在页面加载时,'about' link 处于活动状态并显示为红色。当单击另一个 link 时,导航栏的状态将设置为 'project','about' link 样式将返回,并且 'project' 为红色。
我通过将点击处理程序附加到两个 link 标签来实现这一点,并将活动状态设置为 event.target.innerHTML 的名称。
我是新手,我觉得这是一种非常冗长的处理方式。我知道有一个 activeClassName 道具可以传递给反应路由器 link,但我想要一种不使用它的方法。
import React, { Component } from 'react'
import { Link, Route } from 'react-router'
export default class Navbar extends Component {
constructor() {
super();
this.state = {
active: 'about'
}
this._handleClick = this._handleClick.bind(this);
}
_handleClick(e) {
this.setState({
active: e.target.innerHTML
});
}
render() {
let aboutStyle;
let projectStyle;
if (this.state.active === 'about') {
aboutStyle = { color: '#ff3333' };
projectStyle = {};
} else {
aboutStyle = {};
projectStyle = { color: '#ff3333' };
}
return (
<div className='navbar'>
<Link to='/'><h2>BK //</h2></Link>
<div className='menu'>
<Link style={aboutStyle} onClick={this._handleClick} to='about'>about</Link>
<Link style={projectStyle} onClick={this._handleClick} to='projects'>projects</Link>
</div>
</div>
)
}
}
可能稍微不那么冗长...在伪代码中
const menuItems = [
'projects',
'about',
];
class MenuExample extends React.Component {
_handleClick(menuItem) {
this.setState({ active: menuItem });
}
render () {
const activeStyle = { color: '#ff3333' };
return (
<div className='menu'>
{menuItems.map(menuItem =>
<Link
style={this.state.active === menuItem ? activeStyle : {}}
onClick={this._handleClick.bind(this, menuItem)}
>
{menuItem}
</Link>
)}
</div>
);
}
}
今天您可以使用来自 react-router-dom
的 NavLink。此对象支持 activeClassName
、activeStyle
或 isActive
(对于函数)这样的属性。
import { NavLink } from 'react-router-dom';
<NavLink to='about' activeClassName="active">about</NavLink>
// Or specifing active style
<NavLink to='about' activeStyle={{color: "red"}}>about</NavLink>
// If you use deep routes and you need an exact match
<NavLink exact to='about/subpath' activeClassName="active">about</NavLink>
有关更多选项,请阅读文档:https://reacttraining.com/react-router/web/api/NavLink
使用 NavLink
代替 Link
让您有机会为活动 page/link.
设置您喜欢的样式
所以你可以像这样在 CSS 中设置样式。
.active {
color: 'red'
}
以下 React.js 代码呈现一个导航栏,其中包含两个名为 'about' 和 'project' 的 link。在页面加载时,'about' link 处于活动状态并显示为红色。当单击另一个 link 时,导航栏的状态将设置为 'project','about' link 样式将返回,并且 'project' 为红色。
我通过将点击处理程序附加到两个 link 标签来实现这一点,并将活动状态设置为 event.target.innerHTML 的名称。
我是新手,我觉得这是一种非常冗长的处理方式。我知道有一个 activeClassName 道具可以传递给反应路由器 link,但我想要一种不使用它的方法。
import React, { Component } from 'react'
import { Link, Route } from 'react-router'
export default class Navbar extends Component {
constructor() {
super();
this.state = {
active: 'about'
}
this._handleClick = this._handleClick.bind(this);
}
_handleClick(e) {
this.setState({
active: e.target.innerHTML
});
}
render() {
let aboutStyle;
let projectStyle;
if (this.state.active === 'about') {
aboutStyle = { color: '#ff3333' };
projectStyle = {};
} else {
aboutStyle = {};
projectStyle = { color: '#ff3333' };
}
return (
<div className='navbar'>
<Link to='/'><h2>BK //</h2></Link>
<div className='menu'>
<Link style={aboutStyle} onClick={this._handleClick} to='about'>about</Link>
<Link style={projectStyle} onClick={this._handleClick} to='projects'>projects</Link>
</div>
</div>
)
}
}
可能稍微不那么冗长...在伪代码中
const menuItems = [
'projects',
'about',
];
class MenuExample extends React.Component {
_handleClick(menuItem) {
this.setState({ active: menuItem });
}
render () {
const activeStyle = { color: '#ff3333' };
return (
<div className='menu'>
{menuItems.map(menuItem =>
<Link
style={this.state.active === menuItem ? activeStyle : {}}
onClick={this._handleClick.bind(this, menuItem)}
>
{menuItem}
</Link>
)}
</div>
);
}
}
今天您可以使用来自 react-router-dom
的 NavLink。此对象支持 activeClassName
、activeStyle
或 isActive
(对于函数)这样的属性。
import { NavLink } from 'react-router-dom';
<NavLink to='about' activeClassName="active">about</NavLink>
// Or specifing active style
<NavLink to='about' activeStyle={{color: "red"}}>about</NavLink>
// If you use deep routes and you need an exact match
<NavLink exact to='about/subpath' activeClassName="active">about</NavLink>
有关更多选项,请阅读文档:https://reacttraining.com/react-router/web/api/NavLink
使用 NavLink
代替 Link
让您有机会为活动 page/link.
所以你可以像这样在 CSS 中设置样式。
.active {
color: 'red'
}