• react使用hook封装一个tab组件


    react使用hook封装一个tab组件

    Tabbar.jsx

    import PropsTypes from "prop-types";
    import React, { useEffect, useState } from 'react';
    export default function Tabbar(props) {
      const {  tabData , current } = props
      const [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 (
                    <div
                      style={{
                        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:''
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    使用组件

    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>
        )
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    效果

    在这里插入图片描述

  • 相关阅读:
    2022年9月8号Java23设计模式学习(课时四)建造者模式
    JAVAEE---多线程
    JUnit 5 初探
    Vue3创建项目 Vite
    Backup: MML shutdown 忽略
    包装类 or 泛型
    记录每天学习的新知识:DataBinding
    Linux——文件系统
    Element-Table处理属性列表问题
    未来的嵌入式行业岗位发展怎么样?会不会越来越少?
  • 原文地址:https://blog.csdn.net/weixin_43845137/article/details/132677690