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

React-实现切换tab高亮显示和排序

1、切换className

<div className="tab-list">{ tabList.map(item => (<span key={item.type} className={`tab-box ${item.type === type ? 'active' : ''}`} onClick={() => handleClickTab(item.type)}>{ item.name }</span>))}
</div>

2、classNames优化类名控制

        上面1的写法如果类名非常多的情况下比较复杂,我们可以引入classNames优化类名控制

// 安装
npm i classnames
// 引入
import classNames from 'classnames';// 使用
<div className="tab-list">{ tabList.map(item => (<span key={item.type} className={classNames('tab-box', {active: item.type === type})} onClick={() => handleClickTab(item.type)}>{ item.name }</span>))}
</div>

3、切换时触发重新排序

        以下方法切换tab时不会切换排序,因为这里原地修改原数组,但React的setState需要通过新引用触发重新渲染。由于原数组的引用未改变,React无法感知变化

const [commentList, setCommentList] = useState(list.sort((x, y) => new Date(y.ctime) - new Date(x.ctime)))const [type, setType] = useState('new')
const handleClickTab = (type) => {setType(type)if (type === 'new') {setCommentList(commentList.sort((x, y) => new Date(y.ctime) - new Date(x.ctime)))} else {setCommentList(commentList.sort((x, y) => y.like - x.like))}
}

        修改方法一:

setCommentList([...commentList].sort((x, y) => new Date(y.ctime) - new Date(x.ctime)))

        修改方法二:函数式更新 => 推荐

setCommentList(prev => prev.sort((x, y) => new Date(y.ctime) - new Date(x.ctime)))

3、完整代码

import { useState } from "react";const list = [{ id: 1,user: {uid: '12244',avatar: 'https://img0.baidu.com/it/u=2191392668,814349101&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1399',uname: '周杰伦'},content: '哎哟,不错哦',ctime: '2021-06-08 19:35:47',like: 88},{ id: 2,user: {uid: '12245',avatar: 'https://img0.baidu.com/it/u=2191392668,814349101&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1399',uname: '林俊杰'},content: '好听呀',ctime: '2021-06-15 19:35:47',like: 52},{ id: 3,user: {uid: '12246',avatar: 'https://img0.baidu.com/it/u=2191392668,814349101&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1399',uname: '周笔畅'},content: '呵呵哈哈哈',ctime: '2021-06-02 19:35:47',like: 192}
]const userInfo = {uid: '12245',avatar: 'https://img0.baidu.com/it/u=2191392668,814349101&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1399',uname: '林俊杰'
}const tabList = [{ type: 'new', name: '最新' },{ type: 'hot', name: '最热' },
]function App() {const [commentList, setCommentList] = useState(list.sort((x, y) => new Date(y.ctime) - new Date(x.ctime)))const handleDelete = (id) => {setCommentList(commentList.filter(item => item.id !== id))}const [type, setType] = useState('new')const handleClickTab = (type) => {setType(type)if (type === 'new') {// 不会切换排序,因为这里原地修改原数组,但React的setState需要通过新引用触发重新渲染。由于原数组的引用未改变,React无法感知变化// setCommentList(commentList.sort((x, y) => new Date(y.ctime) - new Date(x.ctime)))// 修改方法一// setCommentList([...commentList].sort((x, y) => new Date(y.ctime) - new Date(x.ctime)))// 修改方法二(函数式更新 => 推荐)setCommentList(prev => prev.sort((x, y) => new Date(y.ctime) - new Date(x.ctime)))} else {// setCommentList(commentList.sort((x, y) => y.like - x.like))// setCommentList([...commentList].sort((x, y) => y.like - x.like))setCommentList(prev => prev.sort((x, y) => y.like - x.like))}}return (<div className="App"><div className="tab-list">{ tabList.map(item => (<span key={item.type} className={`tab-box ${item.type === type ? 'active' : ''}`} onClick={() => handleClickTab(item.type)}>{ item.name }</span>))}</div><div className="user-list">{ commentList.map((item, index) => (<div key={index} className="user-box"><img style={{width: 40, height: 40}} src={item.user.avatar} /><span>{ item.user.uname }</span><div>{ item.content }</div><div>{ item.ctime }</div><span>点赞数:{ item.like }</span>{ userInfo.uid === item.user.uid && <button onClick={() => handleDelete(item.id)}>删除</button> }</div>))}</div></div>);
}export default App;

http://www.xdnf.cn/news/102889.html

相关文章:

  • 【Python爬虫详解】第四篇:使用解析库提取网页数据——BeautifuSoup
  • 240423 leetcode exercises
  • 【Java】HQL查询初步
  • AI健康小屋:解锁健康管理新密码
  • node.js 实战——(概念以及Buffer 知识点学习)
  • AndroidAutomotive模块介绍(四)VehicleHal介绍
  • Minio Linux 安装 systemctl启动配置
  • “信号魔方”大扭转RS232 瞬变 PROFINET 激活交通脉络
  • 文件属性隐写
  • else if 在 C 语言中的使用
  • OJ笔试强训_25至48天_每天三道OJ
  • Vscode已经打开的python项目,如何使用已经建立的虚拟环境
  • TFTP服务调试
  • 网络原理初始
  • opencv--图像滤波
  • OpenCV 图形API(54)颜色空间转换-----将图像从 RGB 色彩空间转换到 HSV色彩空间RGB2HSV()
  • PubLayNet:文档布局分析领域的大规模数据集
  • 科技项目必须进行验收测试吗?项目验收测试服务机构有哪些?
  • 一文读懂https
  • Spark 集群搭建:Standalone 模式详解
  • 组织级项目管理OPM
  • 香港科技大学广州|先进材料学域博士招生宣讲会—南开大学专场
  • 连锁美业管理系统「数据分析」的重要左右分析︳博弈美业系统疗愈系统分享
  • 如何在iStoreOS DHCP中排除特定IP地址
  • 全面解析React内存泄漏:原因、解决方案与最佳实践
  • Oracle EBS R12.2 汉化
  • Oracle 数据库中的 JSON:性能注意事项
  • 单级AC-DC DAB的仿真 2
  • 实时数仓方案介绍
  • jumpserver应用