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

前言:

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

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

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

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

npm 安装 react-virtualized

npm install react-virtualized --save

页面引入

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

Cp.jsx

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;

 效果

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
React-virtualized是一个非常流React库,它可以帮助我们实现大数据量的虚拟滚动效果。下面是一个使用React-virtualized实现虚拟滚动的表格的示例代码: 首先,我们需要安装React-virtualized库: ``` npm install react-virtualized --save ``` 然后,我们需要引入Table和Column组件: ``` import { Table, Column } from 'react-virtualized'; ``` 接下来,我们可以定义一个数据源,例如: ``` const list = [ { name: '张三', age: '18', address: '北京市海淀区' }, { name: '李四', age: '20', address: '北京市朝阳区' }, { name: '王五', age: '22', address: '北京市西城区' }, // ... // 这里可以添加更多的数据 ]; ``` 然后,我们可以定义一个Table组件,指定它的rowCount和rowGetter属性: ``` <Table rowCount={list.length} rowGetter={({ index }) => list[index]} > ``` 接下来,我们可以添加一些Column组件,定义每一列的属性: ``` <Column label="姓名" dataKey="name" width={100} /> <Column label="年龄" dataKey="age" width={100} /> <Column label="地址" dataKey="address" width={200} /> ``` 最后,我们需要在Table组件中添加一些属性,以启用虚拟滚动: ``` <Table rowCount={list.length} rowGetter={({ index }) => list[index]} headerHeight={20} rowHeight={30} width={600} height={400} > ``` 在上面的代码中,我们设置了headerHeight和rowHeight属性来指定表头和每一度,width和height属性用于指定表格的宽度和度。React-virtualized会自动根据这些属性来计算出需要渲染的数,并且只渲染当前可见的,以实现虚拟滚动的效果。 完整的代码示例: ``` import React, { Component } from 'react'; import { Table, Column } from 'react-virtualized'; const list = [ { name: '张三', age: '18', address: '北京市海淀区' }, { name: '李四', age: '20', address: '北京市朝阳区' }, { name: '王五', age: '22', address: '北京市西城区' }, // ... // 这里可以添加更多的数据 ]; class VirtualTable extends Component { render() { return ( <Table rowCount={list.length} rowGetter={({ index }) => list[index]} headerHeight={20} rowHeight={30} width={600} height={400} > <Column label="姓名" dataKey="name" width={100} /> <Column label="年龄" dataKey="age" width={100} /> <Column label="地址" dataKey="address" width={200} /> </Table> ); } } export default VirtualTable; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小灰灰学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值