温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用react实现一个tab组件

发布时间:2022-10-25 17:33:11 来源:亿速云 阅读:121 作者:iii 栏目:web开发

这篇“如何使用react实现一个tab组件”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“如何使用react实现一个tab组件”文章吧。

使用react实现一个tab组件的方法:1、通过“export default props => {...}”方式创建TAB button组件;2、通过“tab-group-layout.js”组件来传“tabIndex”,并设置默认选中的tab效果;3、用react继承“react.component”组件里的onMouseOver和OnMouseOut方法即可。

怎么使用react实现一个tab组件?

react写Tab组件

使用react写TAB栏组件和对应hover事件(背景:在用gatsby开发页面时,遇到这样的组件效果,顺便记录一下)

1、效果

默认选中的tab选中效果 和 鼠标放上去的hover效果

当鼠标滑过右侧的tab时,也会有和第一个一样的选中效果!

2、tab-button.js 组件

import React from "react"
import { css } from "@emotion/core"
import { Link } from "gatsby"
import jdyStyles from "./container.module.css"
 
// TAB button 组件
export default props => {
 
return (
 
<li css={css`font-size: 18px;margin-left:18px;margin-right: 18px;display:flex;flex-direction: column;align-items:center;justify-content:center`} >
 
<Link css={css`font-size: 18px;padding: 20px 12px;`} 
className={ (props.selected?jdyStyles.header_hover_default:jdyStyles.header_hover)  }  to={props.to}>
{props.children}
</Link>
 
</li>
 
)
}

3、tab-group-layout.js 组件

import React from "react"
import { css } from "@emotion/core"
import { Link } from "gatsby"
import ListLink from "../components/tab-button"
import RegisterButton from "../components/round-button"
export default ({ tabIndex }) => {
 
return (
 
<div>
  
{/* tab */}
<ul style={{ listStyle: `none`, float: `right` }} css={css`display: flex;justify-content: space-between;align-items: center;`}>
<ListLink to="/official-site/" selected={(tabIndex==='official-site')}>产品介绍</ListLink>
<ListLink to="/about/" selected={(tabIndex==='about')}>成功案列</ListLink>
<ListLink to="/contact/" selected={(tabIndex==='contact')}>服务支持</ListLink>
<ListLink to="/sweet-pandas-eating-sweets/" selected={(tabIndex==='sweet-pandas-eating-sweets')}>资源中心</ListLink>
</ul>
 
</div>
 
)
}

使用这个组件传过来 tabIndex 设置默认选中的tab效果;也可以自己处理展示的逻辑

4、对应的css样式 container.module.css

.header_hover{
  color: #333;
}
 
.header_hover_default{
  color: #0084ff!important;
  border-top: 3px solid #0084ff;
}
 
.header_hover:hover{
  color: #0084ff!important;
  border-top: 3px solid #0084ff;
}

5、当前组件的hover使用的是css样式控制,也可以用 react继承 react.component组件里的onMouseOver和OnMouseOut方法来实现。

以上就是关于“如何使用react实现一个tab组件”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI