react18【实战】tab切换,纯前端列表排序(含 lodash 和 classnames 的安装和使用)

技术要点

动态样式

js 复制代码
className={`tabItem ${currentType === item.value && "active"}`}

安装 lodash

dos 复制代码
npm i --save lodash

使用 lodash 对对象数组排序(不会改变源数组)

js 复制代码
_.orderBy(dataList, "readNum", "desc")

src\Demo.css

css 复制代码
.tabItem {
    display: inline-block;
    padding: 10px;
    cursor: pointer;
}

.active {
    color: red;
}

.itemBox {
    width: 400px;
    display: flex;
}

.label {
    font-weight: bold;
    padding: 10px 0;
}

.left {
    width: 50%;
}

.center {
    width: 25%;
    text-align: center;
}

.right {
    width: 25%;
    text-align: center;
}

src\Demo.jsx

js 复制代码
import { useState } from "react";
import "./Demo.css";
import _ from "lodash";

function Demo() {
  const dataList_init = [
    {
      id: 1,
      title: "文章1",
      pubTime: "2024/5/1",
      readNum: 9,
    },
    {
      id: 2,
      title: "文章2",
      pubTime: "2024/4/1",
      readNum: 2,
    },
    {
      id: 3,
      title: "文章3",
      pubTime: "2024/5/8",
      readNum: 6,
    },
  ];
  const typeList = [
    {
      value: "new",
      label: "最新",
    },
    {
      value: "hot",
      label: "最热",
    },
  ];

  const [currentType, setCurrentType] = useState("");
  const [dataList, setDataList] = useState(dataList_init);

  function currentTypeChange(newType) {
    setCurrentType(newType);
    if (newType === "hot") {
      // 倒序排列
      setDataList(_.orderBy(dataList, "readNum", "desc"));
    }

    if (newType === "new") {
      // 倒序排列
      setDataList(_.orderBy(dataList, "pubTime", "desc"));
    }
  }
  return (
    <>
      {typeList.map((item) => (
        <div
          className={`tabItem ${currentType === item.value && "active"}`}
          key={item.value}
          onClick={() => currentTypeChange(item.value)}
        >
          {item.label}
        </div>
      ))}

      <div className="itemBox label">
        <div className="left">文章标题</div>
        <div className="center">发布日期</div>
        <div className="right">阅读量</div>
      </div>

      {dataList.map((item) => (
        <div key={item.id} className="itemBox">
          <div className="left">{item.title}</div>
          <div className="center">{item.pubTime}</div>
          <div className="right">{item.readNum}</div>
        </div>
      ))}
    </>
  );
}

export default Demo;

使用 classnames 库改写

classnames 库可以让动态样式的书写更加清晰

安装

dos 复制代码
npm install classnames

使用

js 复制代码
import classNames from "classnames";

js 复制代码
className={`tabItem ${currentType === item.value && "active"}`}

改写为

js 复制代码
className={classNames("tabItem", {
            active: currentType === item.value,
          })}
相关推荐
minhuan9 小时前
大模型Agent核心架构,详解ReAct、Plan-and-Execute、Reason-Act-Observe循环21.6
react·大模型应用·agent架构·ai应用架构设计·plan-execute
名字还没想好☜13 小时前
React useLayoutEffect vs useEffect:页面闪一下的元凶与正确修法
前端·javascript·react.js·react·hooks
钛态3 天前
前端安全防线:CSRF 攻击链路与双重 Token 校验的工程实现
前端·vue·react·web
钛态3 天前
AI 组件生成评测:别只看页面能不能渲染
前端·vue·react·web
赵大仁3 天前
Next.js + Vercel AI SDK 实战:30 分钟搭出流式 Chat 页面
前端·ai·实战·react·next.js·vercel
小小放舟、5 天前
PaiCLI-Demo:从零实现 ReAct Agent + Tool Call
java·后端·intellij-idea·springboot·agent·react·tool call
名字还没想好☜9 天前
React 受控与非受控组件:表单场景怎么选
前端·javascript·react.js·react·表单·受控组件
名字还没想好☜9 天前
Next.js 中间件实战:鉴权、重定向与 A/B 分流
开发语言·前端·javascript·中间件·react·next.js
爱上纯净的蓝天10 天前
使用 AtomCode 构建微前端架构:从设计到实现实战
架构·vue·实战·react·微前端·atomcode
后台开发者Ethan11 天前
setState组件更新
前端·javascript·react