简体   繁体   English

如何在 React 中单击选项卡时使其处于活动状态

[英]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:要在单击时更改活动选项卡,您需要做三件事:

  • Maintain a state to track the active tab and move the tabs array out of the state since they are static.保持状态以跟踪活动选项卡并将选项卡数组移出状态,因为它们是静态的。

 tabs = ["Home", "About", "Contact", "FAQ"]; state = { count: this.props.value, activeTabId: 0 };

  • Change the active tab id when a tab is clicked.单击选项卡时更改活动选项卡 ID。

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

  • Use the changed state in your tab styling function.在选项卡样式功能中使用更改的状态。

 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.

相关问题 使用纯JS单击时如何使选项卡处于活动状态 - How to make a tab active when clicked by using pure JS 使用 JavaScript 单击时使选项卡处于活动状态 - Make Tab active when clicked with JavaScript jQuery:如何记下side-nav的click-tab并在新页面加载时使其变为彩色(活动)? - jQuery: How can I remember clicked-tab of side-nav and make it colored(active) when newly page loaded? angular ui bootstrap选项卡组件:未单击选项卡处于活动状态时如何获取选项卡内容 - angular ui bootstrap tabs component: how to get the content of a tab when the tab is active not clicked 单击/激活时如何使悬停属性保持不变? - How to make the hover attributes stays when it is clicked/active? 仅使列表中单击的项目在反应中处于活动状态 - make only the clicked item on a list active in react 单击选项卡时,如何使这些选项卡的内容淡入? - How would i make the content of these tabs fade in when the tab is clicked? 单击活动选项卡时,手风琴恢复为默认选项卡文本 - Accordion Revert to Default Tab Text When Active Tab is Clicked 从其他页面加载时如何激活选项卡 - how to make a tab active when loaded from different page 激活时如何使我的标签中的正方形变为不同的颜色 - how to make square in my tab a different color when active
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM