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

React实现tab切换

发布时间:2020-12-15 07:27:38 所属栏目:百科 来源:网络整理
导读:下面来编写一个tab选项卡切换效果,效果如下图所示: 下面我放上该组件的代码: import React,{ Component } from 'react' ;import { Link } from 'react-router' ;import '../scss/base.scss' ;import '../scss/tab.scss' ; class TabController extends Co

下面来编写一个tab选项卡切换效果,效果如下图所示:

下面我放上该组件的代码:

import React,{ Component } from 'react';
import { Link } from 'react-router';
import '../scss/base.scss';
import '../scss/tab.scss';

class TabController extends Component {
    constructor(props){
        super(props);
        this.state = {
            current: 0
        };
    }

    itemNav = (index) => {
        return index === this.state.current ? 'item-title active' : 'item-title';
    }

    itemCon = (index) => {
        return index === this.state.current ? 'con active' : 'con';
    }

    render(){
        return (
            <div>
                <ul className="itemContainer clearfix">
                    {
                        React.Children.map(this.props.children,(element,index) => {
                            return (
                                <li onClick={ () => { this.setState({ current: index }) } } className={ this.itemNav(index) }>{ element.props.name }</li>
                            )
                        })
                    }
                </ul>
                <div className="itemCon">
                    {
                        React.Children.map(this.props.children,index) => {
                            return (
                                <div onClick={ () => { this.setState({ current: index }) } } className={ this.itemCon(index) }>{ element }</div>
                            )
                        })
                    }
                </div>
            </div>
        );
    }
}

export default class TabComponent extends Component {
    constructor(props){
        super(props);
        this.state = {

        };
    }

    render(){
        return (
            <TabController>
                <div name="one">
                    第一部分
                </div>
                <div name="two">
                    第二部分
                </div>
                <div name="three">
                    第三部分
                </div>
            </TabController>
        );
    }
}

参考: http://www.cnblogs.com/tianheila/p/5170330.html

(编辑:李大同)

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

    推荐文章
      热点阅读