当前位置: 首页 > news >正文

react使用hook封装一个tab组件

目录

  • react使用hook封装一个tab组件
    • Tabbar.jsx
    • 使用组件
    • 效果

react使用hook封装一个tab组件

Tabbar.jsx

import PropsTypes from "prop-types";
import React, { useEffect, useState } from 'react';
export default function Tabbar(props) {const {  tabData , current } = propsconst [currentTab,setCurrentTab] = useState('test')const tabchange = (item) => {setCurrentTab(item.key)}useEffect(()=>{setCurrentTab(current)},[current])return (<div style={{display: 'flex',justifyContent: 'space-around',alignItems: 'center'}}>{tabData.map(item=>{return (<divstyle={{flex:'1',textAlign:'center',marginRight:'5px',cursor:'pointer',color: item.key === currentTab ? 'red' : '#000',background: item.key === currentTab ? '#ccc' : '#eee',}}key={item.key}onClick={ () => tabchange(item) }>{ item.name }</div>)})}</div>)
}Tabbar.propTypes = {current:  PropsTypes.oneOfType([PropsTypes.string, PropsTypes.number]),tabData: PropsTypes.array.isRequired
};
Tabbar.defaultProps = {tabData: [{name:'测试1',key:'test1'},{name:'测试2',key:'test2'},],current:''
};

使用组件

import React from 'react';
import Tabbar from "../tabbar/Tabbar";
export default function App(props) {const tabData = [{name:'篮球',key:'1'},{name:'足球',key:'2'},{name:'排球',key:'3'}]const current = '2'return (<div className='content'><Tabbar tabData={tabData} current={current} ></Tabbar><div style={{marginBottom: '10px'}}></div><Tabbar></Tabbar></div>)
}

效果

在这里插入图片描述

http://www.lryc.cn/news/160904.html

相关文章:

  • java详细安装教程(供参考)一一java(jdk)安装
  • C++函数内联详解
  • Revit SDK 介绍:NewForm 新建体量
  • Ubuntu离线或在线安装Python解释器
  • 微信小程序隐私协议相关接口实际使用方式
  • MySQL--MySQL表的增删改查(进阶)
  • Golang 中的静态类型和动态类型
  • docker的数据卷、docker数据持久化
  • 阅读源码工具Sourcetrail
  • KMP 算法详解
  • [matconvnet]matconvnet-1.0-beta-25在cuda11.1以上编译问题总结
  • 自动化驱动程序管理
  • 智能合约编写高级篇(二)区块哈希介绍
  • 二进制链表转整数
  • Python爬虫进阶:使用Scrapy库进行数据提取和处理
  • 五)Stable Diffussion使用教程:文生图之高清修复
  • SQL SERVER 如何实现UNDO REDO 和PostgreSQL 有近亲关系吗
  • SpringBoot原理-自动配置-原理分析-源码跟踪
  • 安全基础 --- 原型链污染
  • c++中的常用知识点总结
  • Leetcode:349. 两个数组的交集【题解超详细】
  • Java 【异常】
  • B - Polycarp‘s Practice
  • 朴素贝叶斯数据分类------
  • flask中的操作数据库的插件Flask-SQLAlchemy
  • arrow的使用
  • 【24种设计模式】装饰器模式(Decorator Pattern(Wrapper))
  • 小程序v-for与key值使用
  • Qt包含文件不存在问题解决 QNetworkAccessManager
  • 【视频图像篇】FastStone Capture屏幕长截图软件