[英]How to make a tab active when it is clicked on in React
Good day, I'm trying to write code to make a tab active when it is clicked on in my React App with Bootstrap.美好的一天,我正在尝试编写代码以在使用 Bootstrap 的 React 应用程序中单击选项卡时使其处于活动状态。 I had previously set only the Home Tab to be active but now I want to change the code.
我之前只将主页选项卡设置为活动状态,但现在我想更改代码。 Please how do I go about this?
请问我该怎么办? I am new to React.
我是 React 的新手。
/* eslint-disable jsx-a11y/anchor-is-valid */
import React, { Component } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCoffee, faCut } from '@fortawesome/free-solid-svg-icons';
class Counter extends Component {
state = {
count: this.props.value,
tabs:["Home","About","Contact","FAQ"]
};
assets={
images:{
image1:"images/la.jpg",
image2:"images/chicago.jpg",
image3:"images/ny.jpg"
},
font:{
font1:"Roboto",
font2:"sans-serif",
font3:"Verdana"
},
styles:{
fontSize:12,
fontWeight:'bold',
fontColor:'whitesmoke'
},
imgstyle:{
borderRadius:"50%",
width:"100px",
height:"100px",
marginTop:"10px"
}
}
formatCount=()=>{
const count=this.state.count;
return count===0?"Zero":count;
};
handleIncrement=()=>{
this.setState({count:this.state.count + 1})
};
styles={
fontSize:15,
fontWeight:"bold"
}
renderTags=()=>{
return(
<nav className="nav nav-tabs">
{this.state.tabs.map(tab=>(
<a href="#" key={tab} className={this.activeNav(tab)}>
{tab}
</a>
))}
</nav>
);
};
formatBadge=()=>{
let classes="badge m-2 badge-";
classes+=this.state.count===0?"warning":"success";
return classes;
};
/*MAKES HOME TAB ACTIVE*/
activeNav=(navlink)=>{
if (navlink==="Home"){
return "nav-item nav-link active"}
else{
return "nav-item nav-link"}
};
render() {
console.log('props',this.props.value)
return (
<div>
<span style={this.styles} className={this.formatBadge()}>
{this.formatCount()}
</span>
<button onClick={this.handleIncrement} className="btn btn-sm btn-secondary m-2"><FontAwesomeIcon icon={faCoffee} className="mr-2"/>Increment</button>
<button className="btn btn-danger btn-sm m-2" onClick={()=>this.props.deleteThis(this.props.id)}><FontAwesomeIcon icon={faCut} className="mr-2"/>Delete</button>
<img style={this.assets.imgstyle} src={this.assets.images.image1} alt=""/>
{this.renderTags()}
</div>
);
}
}
export default Counter;
Any help regarding this would be appreciated.对此的任何帮助将不胜感激。 Thank you.
谢谢你。
To change the active tab when you click on it, there are three things you need to do:要在单击时更改活动选项卡,您需要做三件事:
tabs = ["Home", "About", "Contact", "FAQ"]; state = { count: this.props.value, activeTabId: 0 };
onClick = (id) => { this.setState({ ...this.state, activeTabId: id }); }; renderTags = () => { return ( <nav className="nav nav-tabs"> {this.tabs.map((tab, id) => ( <a href="#" key={tab} onClick={() => this.onClick(id)} className={this.activeNav(id)} > {tab} </a> ))} </nav> ); };
activeNav = (id) => { if (id === this.state.activeTabId) { return "nav-item nav-link active"; } else { return "nav-item nav-link"; } };
You can find the full code here .您可以在此处找到完整代码。
Having said that, there are a couple of things you could do better.话虽如此,有几件事你可以做得更好。 You could just ditch the class and use functions for component and use react hooks for handling your states once you do that.
一旦你这样做,你可以放弃类并使用组件的函数并使用反应钩子来处理你的状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.