虛擬列表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.pswp.cn/web/75161.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/75161.shtml
英文地址,請注明出處:http://en.pswp.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>…