虚拟列表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 (


    {item.name}


    {item.description}



    );
    };

    复制代码
     return (
         <List
             width={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 (
             <CellMeasurer
                 key={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 (
         <List
             width={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 (


    Loading...

    );
    }

    复制代码
     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 }) => (
    <List
    width={width}
    height={height}
    rowCount={items.length + 1} // +1 for loading row
    rowHeight={50}
    rowRenderer={rowRenderer}
    onRowsRendered={onRowsRendered}
    overscanRowCount={5}
    />
    )}
    </AutoSizer>
    {loading &&

    Loading more items...
    }

);
}

export default InfiniteLoadingList;

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

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

  • 相关推荐
    耶啵奶膘2 小时前
    uniapp+firstUI——上传视频组件fui-upload-video
    前端·javascript·uni-app
    视频砖家2 小时前
    移动端Html5播放器按钮变小的问题解决方法
    前端·javascript·viewport功能
    lyj1689973 小时前
    vue-i18n+vscode+vue 多语言使用
    前端·vue.js·vscode
    小白变怪兽4 小时前
    一、react18+项目初始化(vite)
    前端·react.js
    ai小鬼头4 小时前
    AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
    前端·后端·github
    墨菲安全5 小时前
    NPM组件 betsson 等窃取主机敏感信息
    前端·npm·node.js·软件供应链安全·主机信息窃取·npm组件投毒
    GISer_Jing5 小时前
    Monorepo+Pnpm+Turborepo
    前端·javascript·ecmascript
    天涯学馆5 小时前
    前端开发也能用 WebAssembly?这些场景超实用!
    前端·javascript·面试
    我在北京coding6 小时前
    TypeError: Cannot read properties of undefined (reading ‘queryComponents‘)
    前端·javascript·vue.js
    前端开发与ui设计的老司机7 小时前
    UI前端与数字孪生结合实践探索:智慧物流的货物追踪与配送优化
    前端·ui