这几天在学习react框架,组件化的思想真的很酷。下面总结一下一个简单react tab切换组件的实现过程。

项目源码:react-tab

组件核心代码

import React from "react"
import "../css/style.css" class TabsControl extends React.Component{
constructor( ){
super( )
this.state = {
currentIndex :
}
} check_title_index( index ){
return index === this.state.currentIndex ? "tab_title active" : "tab_title"
} check_item_index( index ){
return index === this.state.currentIndex ? "tab_item show" : "tab_item"
} render( ){
let _this = this
return(
<div>
{ /* 动态生成Tab导航 */ }
<div className="tab_title_wrap">
{
React.Children.map( this.props.children , ( element,index ) => {
return(
<div onClick={ ( ) => { this.setState({ currentIndex : index }) } } className={ this.check_title_index( index ) }>{ element.props.name }</div>
)
})
}
</div>
{ /* Tab内容区域 */ }
<div className="tab_item_wrap">
{
React.Children.map(this.props.children,( element,index )=>{
return(
<div className={ this.check_item_index( index ) }>{ element }</div>
)
})
}
</div>
</div>
)
}
} export default TabsControl

组件使用

import React from "react"
import ReactDOM from "react-dom"
import TabsControl from "./react_tab.jsx" class TabComponent extends React.Component{
render( ){
return(
<div className="container">
<TabsControl>
<div name = "first">
第一帧
</div>
<div name = "second">
第二帧
</div>
<div name = "third">
第三帧
</div>
</TabsControl>
</div>
)
}
} ReactDOM.render(<TabComponent/>,document.getElementById("app"))

实现思路:

在使用<TabsControl/>组件时会传入任意数量的div,即为切换组件的主要内容帧,在组件内部通过 this.props.children 获取到主要内容帧,并且动态生成相应数量的tab_title,再对标题区和内容区设置合适的className,以控制标题区的颜色切换和内容区域的显示和隐藏,组件通过 state 存放 index 来记忆被点击的区域,并且每一个标题区域都有绑定一个 click 处理方法,每一次点击都会更新 state 的 index 值,组件会自动调用 this.render 方法重新渲染视图,上面说到的 className 的设置规则也是借由index值来实现的 => 当标题区域或者内容区域其对应的索引值与 state 中的 index 相同的时候,给它们添加具有特殊的即动态标示的className,使得当前被点击标题高亮以及对应的内容帧显现。

react实现的tab切换组件的更多相关文章

  1. react简单的tab切换 (styled-components)

    其实,在我们日常的编程当中,经常会碰到tab切换的模块,那么,实现他的方法也有很多中,下面是一款简单,容易理解的react tab切换方法. 通过设置state中的current 属性去控制tab 和 ...

  2. Flutter——TabBar组件(顶部Tab切换组件)

    TabBar组件的常用属性: 属性 描述 tabs 显示的标签内容,一般使用 Tab 对象,也可以是其他的Widget  controller TabController 对象 isScrollabl ...

  3. 简单的Tab切换组件

    由于代码都有注释,所以不多加解释,大家都知道的.直接贴代码: 代码如下: /** * 简单的Tab切换 * 支持可配置项 如下参数 */ function Tab(){ this.config = { ...

  4. 基于Vue开发的tab切换组件

    github地址:https://github.com/MengFangui/VueTabSwitch 1.index.html <!DOCTYPE html> <html lang ...

  5. tab切换组件nz-tab

    <nz-card [nzBordered]="true" nzTitle="卡片标题"> <nz-card style="width ...

  6. 使用VUE搭建tab标签组件

    Vue2.0 多 Tab切换组件简单封装,满足自己简单的功能,可以直接拿去使用! 首先上效果图: 功能简单介绍: 1.支持tab切换 2.支持tab定位 3.支持tab自动化 仿React多Tab实现 ...

  7. vue路由+vue-cli实现tab切换

    第一步:搭建环境 安装vue-cli cnpm install -g vue-cli安装vue-router cnpm install -g vue-router使用vue-cli初始化项目 vue ...

  8. 【原】react做tab切换的几种方式

    最近搞一个pc端的活动,搞了一个多月,甚烦,因为相比于pc端,更喜欢移动端多一点.因为移动端又能搞我的react了. 今天主要总结一下react当中tab切换的几种方式,因为tab切换基本上都会用到. ...

  9. React Native 系列(九) -- Tab标签组件

    前言 本系列是基于React Native版本号0.44.3写的.很多的App都使用了Tab标签组件,例如QQ,微信等等,就是切换不同的选项,显示不同的内容.那么这篇文章将介绍RN中的Tab标签组件. ...

随机推荐

  1. 查看linux系统,服务,配置文件被修改的时间

    如何查看服务启动时间 [root@qike /]# ps -ef |grep nginx root 14730 1 0 16:45 ? 00:00:00 nginx: master process / ...

  2. Neutron VxLAN + Linux Bridge 环境中的网络 MTU

    1. 基础知识 1.1 MTU   一个网络接口的 MTU 是它一次所能传输的最大数据块的大小.任何超过MTU的数据块都会在传输前分成小的传输单元.MTU 有两个测量层次:网络层和链路层.比如,网络层 ...

  3. 学习OpenStack之 (4): Linux 磁盘、分区、挂载、逻辑卷管理 (Logical Volume Manager)

    0. 背景: inux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间.普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻 ...

  4. LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]

    题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  5. POJ1849Two[DP|树的直径](扩展HDU4003待办)

    Two Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1390   Accepted: 701 Description Th ...

  6. JSP中九大内置对象及其数据类型

    JSP中九大内置对象为: request              请求对象               类型 javax.servlet.ServletRequest         作用域 Req ...

  7. java 26 - 8 网络编程之 TCP协议的练习

    TCP练习: 1.客户端键盘录入,服务器输出文本文件 客户端代码: public class ClientDemo { public static void main(String[] args) t ...

  8. 使用javascript对密码进行有密码强度提示的验证

    好些网站的注册功能中,都有对密码进行验证并且还有强度提示.下面就来实现这种效果.密码强度说明:密码强度:弱——纯数字,纯字母,纯符号密码强度:中——数字,字母,符号任意两种的组合密码强度:强——数字, ...

  9. HTML 学习笔记 CSS3 (2D转换)

    2.scaleX(<number>) : 使用 [sx,1] 缩放矢量执行缩放操作,sx为所需参数.scaleX表示元素只在X轴(水平方向)缩放元素,他的默认值是(1,1),其基点一样是在 ...

  10. linux下截取给定路径中的目录部分

    在日常运维中,有时会要求截取一个路径中的目录部分.截取目录的方法,有以下两种:1)dirname命令(最常用的方法):用于取给定路径的目录部分.很少直接在shell命令行中使用,一般把它用在shell ...