虚拟列表react-virtualized使用(npm install react-virtualized)

1. 虚拟化列表 (List)

// 1. 虚拟化列表 (List)import { List } from 'react-virtualized';
import 'react-virtualized/styles.css'; // 只导入一次样式// 示例数据
const list = Array(1000).fill().map((_, index) => ({id: index,name: `Item ${index}`,description: `This is item number ${index} in the list`
}));function Index() {const rowRenderer = ({ index, key, style }) => {const item = list[index];return (<div key={key} style={style} className="list-item"><h3>{item.name}</h3><p>{item.description}</p></div>);};return (<Listwidth={600} // 列表宽度height={400} // 列表高度rowCount={list.length} // 总行数rowHeight={80} // 每行高度rowRenderer={rowRenderer} // 行渲染函数overscanRowCount={5} // 预渲染的行数/>)
}export default Index;

2. 可变高度列表 (CellMeasurer)

// 2. 可变高度列表 (CellMeasurer)import { List, CellMeasurer, CellMeasurerCache } from 'react-virtualized';
import 'react-virtualized/styles.css';// 可变高度数据
const variableData = Array(500).fill().map((_, index) => ({id: index,title: `Item ${index}`,content: `This is item ${index}. `.repeat(Math.floor(Math.random() * 10) + 1)
}));function Index() {// 创建测量缓存const cache = new CellMeasurerCache({defaultHeight: 60,fixedWidth: true});const rowRenderer = ({ index, key, parent, style }) => {const item = variableData[index];return (<CellMeasurerkey={key}cache={cache}parent={parent}columnIndex={0}rowIndex={index}><div style={style} className="variable-item"><h3>{item.title}</h3><p>{item.content}</p></div></CellMeasurer>);};return (<Listwidth={600} // 列表宽度height={400} // 列表高度deferredMeasurementCache={cache}rowHeight={cache.rowHeight} // 每行高度rowCount={variableData.length} // 总行数rowRenderer={rowRenderer} // 行渲染函数overscanRowCount={3} // 预渲染的行数/>)
}export default Index;

3. 无限加载列表 - 高度固定

// 3. 无限加载列表 - 高度固定import React, { useState } from 'react';
import { List, AutoSizer } from 'react-virtualized';
import 'react-virtualized/styles.css';function InfiniteLoadingList() {const [items, setItems] = useState(Array(50).fill().map((_, i) => ({ id: i, name: `Item ${i}` })));const [loading, setLoading] = useState(false);const loadMoreItems = () => {if (loading) return;setLoading(true);// 模拟API调用setTimeout(() => {const newItems = Array(50).fill().map((_, i) => ({id: items.length + i,name: `Item ${items.length + i}`}));setItems(prev => [...prev, ...newItems]);setLoading(false);}, 1000);};const isRowLoaded = ({ index }) => index < items.length;const rowRenderer = ({ index, key, style }) => {if (!isRowLoaded({ index })) {return (<div key={key} style={style} className="loading-row">Loading...</div>);}const item = items[index];return (<div key={key} style={style} className="list-item">{item.name}</div>);};const onRowsRendered = ({ stopIndex }) => {if (stopIndex >= items.length - 10 && !loading) {loadMoreItems();}};return (<div style={{ height: '500px', width: '100%' }}><AutoSizer>{({ width, height }) => (<Listwidth={width}height={height}rowCount={items.length + 1} // +1 for loading rowrowHeight={50}rowRenderer={rowRenderer}onRowsRendered={onRowsRendered}overscanRowCount={5}/>)}</AutoSizer>{loading && <div className="loading-indicator">Loading more items...</div>}</div>);
}export default InfiniteLoadingList;

4. 无限加载列表 - 高度不固定

// 4. 无限加载列表 - 高度不固定

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/75161.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IT+开发+业务一体化:AI驱动的ITSM解决方案Jira Service Management价值分析(文末免费获取报告)

本文来源atlassian.com&#xff0c;由Atlassian全球白金合作伙伴、DevSecOps解决方案提供商-龙智翻译整理。 无论是支持内部员工、处理突发事件还是批准变更申请&#xff0c;服务团队的每一分钟都至关重要。您的企业是否做好了充分准备&#xff1f; 许多企业仍然依赖传统的IT服…

leetcode刷题日记——167. 两数之和 II - 输入有序数组

[ 题目描述 ]&#xff1a; [ 思路 ]&#xff1a; 题目要求求数值numbers中的和为 target 的两个数的下标最简单的思路就是暴力求解&#xff0c;两两挨个组合&#xff0c;但时间复杂度为O(n2)&#xff0c;不一定能通过因为数组为非递减&#xff0c;那我们可以使用双指针&#…

【Leetcode-Hot100】盛最多水的容器

题目 解答 目的是求面积最大&#xff0c;面积是由两个下标和对应的最小值得到&#xff0c;因此唯一的问题就是如何遍历这两个下标。我采用begin和end两个变量&#xff0c;确保begin是小于end的&#xff0c;使用它们二者求面积&#xff0c;代码如下&#xff1a; 很不幸 出错了…

dify文本生成图片

安装Stability 授权 Stability AI - Developer Platform Stability AI - Developer Platform 创建智能体 模型要选好点的&#xff0c;要不可能会生成失败。

前端开发中的问题排查与定位:HTML、CSS、JavaScript(报错的解决方式)

目录 1.html 1. 结构错误调试&#xff1a;标签未正确嵌套 2. 语法问题调试&#xff1a;缺失引号 3. 断点调试&#xff1a;动态生成内容时的 JavaScript 错误 4. 网络调试&#xff1a;资源加载错误 5. 性能调试&#xff1a;页面加载性能 总结&#xff1a; 2.CSS 1. 定位…

Spring MVC 重定向(Redirect)详解

Spring MVC 重定向&#xff08;Redirect&#xff09;详解 1. 核心概念与作用 重定向&#xff08;Redirect&#xff09; 是 Spring MVC 中一种客户端重定向机制&#xff0c;通过 HTTP 302 状态码&#xff08;默认&#xff09;将用户浏览器重定向到指定 URL。 主要用途&#xf…

《深入探秘:分布式软总线自发现、自组网技术原理》

在当今数字化浪潮中&#xff0c;分布式系统的发展日新月异&#xff0c;而分布式软总线作为实现设备高效互联的关键技术&#xff0c;其自发现与自组网功能宛如打开智能世界大门的钥匙&#xff0c;为多设备协同工作奠定了坚实基础。 分布式软总线的重要地位 分布式软总线是构建…

eplan许可证的用户权限管理

在电气设计领域&#xff0c;EPLAN软件以其强大的功能和灵活性而备受用户青睐。然而&#xff0c;随着企业规模的扩大和团队人数的增加&#xff0c;如何确保软件使用的安全与效率成为了一个重要的问题。EPLAN许可证的用户权限管理功能为此提供了完美的解决方案。本文将详细介绍EP…

pytorch小记(十七):PyTorch 中的 `expand` 与 `repeat`:详解广播机制与复制行为(附详细示例)

pytorch小记&#xff08;十七&#xff09;&#xff1a;PyTorch 中的 expand 与 repeat&#xff1a;详解广播机制与复制行为&#xff08;附详细示例&#xff09; &#x1f680; PyTorch 中的 expand 与 repeat&#xff1a;详解广播机制与复制行为&#xff08;附详细示例&#xf…

Databricks: Why did your cluster disappear?

You may found that you created a cluster many days ago, and you didnt delete it, but it is disapear. Why did this happen? Who deleted the cluster? Actually, 30 days after a compute is terminated, it is permanently deleted automaticlly. If your workspac…

C语言【输出字符串中的大写字母】

题目 输出字符串中的大写字母 思路&#xff08;注意事项&#xff09; 纯代码 #include<stdio.h> #include<string.h>int main(){char str[20], ans[20];fgets(str, sizeof(str), stdin);str[strcspn(str, "\n")] \0;for (int i 0, j 0; i < strl…

基于队列构建优先级抢占机制的LED灯框架设计与实现

文章目录 前言一、LED 显示框架概述1. 框架结构图2. 基本机制 二、核心结构与接口设计1. 状态命令结构2. 状态项结构3. LED框架配置结构4. LED运行控制器 三、LED框架逻辑流程1. 初始化逻辑2. 优先级抢占判断与处理逻辑3. 执行队列命令并处理tick4. 队列为空时的默认状态回滚 四…

PyQt6实例_A股财报数据维护工具_解说并数据与完整代码分享

目录 1 20250403之前的财报数据 2 整个项目代码 3 工具使用方法 3.1 通过akshare下载 3.2 增量更新 3.3 查看当前数据情况 3.4 从数据库中下载数据 视频 1 20250403之前的财报数据 通过网盘分享的文件&#xff1a;财报三表数据20250403之前.7z 链接: https://pan.ba…

React 之 Redux 第三十一节 useDispatch() 和 useSelector()使用以及详细案例

使用 Redux 实现购物车案例 由于 redux 5.0 已经将 createStore 废弃&#xff0c;我们需要先将 reduxjs/toolkit 安装一下&#xff1b; yarn add reduxjs/toolkit// 或者 npm install reduxjs/toolkit使用 vite 创建 React 项目时候 配置路径别名 &#xff1a; // 第一种写法…

Spring Boot 中集成 Knife4j:解决文件上传不显示文件域的问题

Spring Boot 中集成 Knife4j&#xff1a;解决文件上传不显示文件域的问题 在使用 Knife4j 为 Spring Boot 项目生成 API 文档时&#xff0c;开发者可能会遇到文件上传功能不显示文件域的问题。本文将详细介绍如何解决这一问题&#xff0c;并提供完整的解决方案。 Knife4j官网…

OpenCV 图形API(17)计算输入矩阵 src 中每个元素的平方根函数sqrt()

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 描述 计算数组元素的平方根。 cv::gapi::sqrt 函数计算每个输入数组元素的平方根。对于多通道数组&#xff0c;每个通道会独立处理。其精度大约与内置的 …

大学论文书写规范与格式说明

大学论文书写规范与格式说明 (适用于人文社科、理工科通用框架) 一、论文整体结构 1. 基本组成部分 封面 包含论文标题、作者姓名、学院/专业、学号、指导教师、提交日期等(按学校模板填写)。 中英文摘要 中文摘要:300~500字,概述研究背景、方法、结论与创新点,末尾附…

C# 串口通信

1. 导入 using System.IO.Ports;2. 初始化定义 SerialPort sp new SerialPort(); // 设置串口 sp.PortName "COM3"; // 串口 sp.BaudRate 9600; // 波特率 sp.Parity Parity.None; // 校验位 sp.DataBits 8; // 数据位 sp.StopBits StopBits.One; // 停…

android14 keycode 上报 0 解决办法

驱动改完后发现上报了keycode=0 04-07 13:02:33.201 2323 2662 D WindowManager: interceptKeyTq keycode=0 interactive=false keyguardActive=true policyFlags=2000000 04-07 13:02:33.458 2323 2662 D WindowManager: interceptKeyTq keycode=0 interactive=false key…

C++day9

思维导图 牛客练习 练习&#xff1a; 将我们写的 myList 迭代器里面 operator[] 和 operator 配合异常再写一遍 #include <iostream> #include <cstring> #include <cstdlib> #include <unistd.h> #include <sstream> #include <vector>…