react-virtualized实现行元素不等高的虚拟列表滚动

前言:

当一个页面中需要接受接口返回的全部数据进行页面渲染时间,如果数据量比较庞大,前端在渲染dom的过程中需要花费时间,造成页面经常出现卡顿现象。

需求:通过虚拟加载,优化页面渲染速度

优点:不需要固定行元素高度一致

行元素等高的虚拟列表实现方法
实现方法

npm 安装 react-virtualized

npm install react-virtualized --save

页面引入

import { List as VirtualizedList, AutoSizer, CellMeasurer, CellMeasurerCache } from 'react-virtualized';

Cp.jsx

javascript 复制代码
import React, { Component } from 'react';
import { List as VirtualizedList, AutoSizer, CellMeasurer, CellMeasurerCache } from 'react-virtualized';

class Cp extends Component {
  constructor(props) {
    super(props);
    this.cache = new CellMeasurerCache({
      fixedWidth: true, 
      defaultHeight: 100 // 未计算的单元格初始默认的高度
    });

    this.state = {
      viewWidth: 200,
      viewHeight: 500,
      dataList: [
        {id: 1, content1: '小灰灰学编程小灰灰学编程小灰灰学编程', content2: '小灰灰学编程小灰灰学编程小灰灰学编程小灰灰学编程'},
        {id: 2, content1: '小灰灰学编程', content2: '小灰灰学编程小灰灰学编程小灰灰学编程小灰灰学编程'},
        {id: 3, content1: '小灰灰学编程小灰灰学编程', content2: ''},
        {id: 4, content1: '小灰灰学编程小灰灰学编程小灰灰学编程小灰灰学编程', content2: '小灰灰学编程'},
        {id: 5, content1: '小灰灰学编程', content2: ''},
        {id: 6, content1: '小灰灰学编程小灰灰学编程', content2: '小灰灰学编程'},
        {id: 7, content1: '小灰灰学编程小灰灰学编程小灰灰学编程', content2: '小灰灰学编程'},
        {id: 8, content1: '小灰灰学编程小灰灰学编程小灰灰学编程小灰灰学编程小灰灰学编程小灰灰学编程小灰灰学编程', content2: '小灰灰学编程小灰灰学编程小灰灰学编程小灰灰学编程小灰灰学编程小灰灰学编程'},
        {id: 9, content1: '小灰灰学编程小灰灰学编程', content2: ''},
        {id: 10, content1: '小灰灰学编程', content2: '小灰灰学编程'},
        {id: 7, content1: '小灰灰学编程', content2: '小灰灰学编程小小灰灰学编程小灰灰学编程小灰灰学编程灰小灰灰学编程灰学编程'},
        {id: 8, content1: '小灰灰学编程', content2: '小灰灰学编程小小灰灰学编程灰灰学编程'},
        {id: 9, content1: '小灰灰学编程小灰灰学编程', content2: ''},
        {id: 10, content1: '小灰灰学编程', content2: '小灰灰学编程'},
      ]
    }
  }

  componentDidUpdate() {
    // 重置所有单元格的计算缓存
    this.cache.clearAll();
  }

  /**
   * rowRenderer 渲染行元素的方法
   *    index: 索引
   *    key: 记录在数组中的位置
   *    parent: 定义该列表是另一个列表的父列表还是子列表
   *    style: 用于定位行的样式对象
   *    isVisible: 确定行是否可见或不可见
   *    isScrolling: 指示组件中是否发生滚动 List
   * 
   * CellMeasurer 自动计算单元格内容的高阶组件
   *    cache: 在CellMeasure和他们父级的Grid之间共享的缓存
   *    children: 子元素 可以是一个react元素或者函数
   *    columnIndex: 经计算的列index | 0
   *    parent: 父级Grid的引用
   *    rowIndex: 经计算的行index
   */

  rowRenderer = ({ index, key, parent, style }) =>{
    const {dataList} = this.state
    const itemData = dataList[index];
    return (
      <div key={key} style={style}>
        <CellMeasurer
          cache={this.cache}
          columnIndex={0}
          key={key}
          rowIndex={index}
          parent={parent}
        >
          <div>
            <div>{`第${itemData.id}个元素`}</div>
            <div>content1: {itemData.content1}</div>
            <div>{itemData.content2}</div>
          </div>
        </CellMeasurer>
      </div>
    );
  }

  render() {
    const {dataList} = this.state
    return (
      <div className="virtualized-list">
        <AutoSizer>
          {({ viewWidth, viewHeight }) => (
            /**
             * VirtualizedList
             *    width: 可视区域宽度
             *    height: 可视区域高度
             *    rowHeight: 行高度
             *    rowCount: 列表长度
             *    dataList: 列表数据
             *    rowRenderer: 渲染行元素的方法
             *    overscanRowCount: 用于沿用户滚动的方向呈现附加行
             *    deferredMeasurementCache: 用于临时呈现数据,缓存计算数据
             */
            <VirtualizedList
              width={this.state.viewWidth}
              height={this.state.viewHeight}
              rowHeight={this.cache.rowHeight}
              rowCount={dataList.length}
              rowRenderer={this.rowRenderer}
              deferredMeasurementCache={this.cache}
              dataList={dataList}
              overscanRowCount={1}
            />
          )} 
        </AutoSizer>
      </div>
    );
  }
}

export default Cp;

效果

相关推荐
Coding的叶子3 小时前
React Flow 节点事件处理实战:鼠标 / 键盘事件全解析(含节点交互代码示例)
react.js·交互·鼠标事件·fgai·react agent
it_remember9 小时前
新建一个reactnative 0.72.0的项目
javascript·react native·react.js
ZHOU_WUYI14 小时前
使用 Docker 部署 React + Nginx 应用教程
nginx·react.js·docker
互联网搬砖老肖18 小时前
React组件(一):生命周期
前端·javascript·react.js
小马哥编程18 小时前
React和Vue在前端开发中, 通常选择哪一个
前端·vue.js·react.js
OK_boom21 小时前
React-useRef
javascript·react.js·ecmascript
GISer_Jing1 天前
React底层架构深度解析:从虚拟DOM到Fiber的演进之路
前端·react.js·架构
邝邝邝邝丹1 天前
React学习———React Router
前端·学习·react.js
郝开1 天前
扩展:React 项目执行 yarn eject 后的 package.json 变化详解及参数解析
react.js·前端框架·react
Coding的叶子2 天前
Node.js 安装 + React Flow 快速入门:环境安装与项目搭建
react.js·node.js·react flow·fgai·react agent