2026-01-29 15:39:44
针对 Ant Design 3.x Table 大数据量渲染慢的问题,可通过模拟虚拟列表效果、优化渲染性能及合理选用第三方库进行优化,具体方法如下:
自定义模拟虚拟列表Ant Design 3.x 版本未内置虚拟列表功能,需通过自定义渲染模拟该效果,核心步骤如下:
示例代码片段:
class VirtualTable extends React.Component { constructor(props) { super(props); this.tableRef = React.createRef(); this.state = { scrollTop: 0, visibleData: [], }; } handleScroll = () => { const { scrollTop } = this.tableRef.current; this.setState({ scrollTop }, () => { this.updateVisibleData(); }); }; updateVisibleData = () => { const { originalData, rowHeight } = this.props; const { scrollTop } = this.state; const tableHeight = this.tableRef.current.clientHeight; const visibleRowCount = Math.ceil(tableHeight / rowHeight); const startIndex = Math.floor(scrollTop / rowHeight); const endIndex = startIndex + visibleRowCount; this.setState({ visibleData: originalData.slice(startIndex, endIndex) }); }; render() { const { columns, rowHeight } = this.props; const { visibleData } = this.state; return ( <div ref={this.tableRef} onScroll={this.handleScroll} style={{ height: '500px', overflow: 'auto' }} > <Table columns={columns} dataSource={visibleData} pagination={false} scroll={{ y: rowHeight * this.props.originalData.length }} rowKey={(record) => record.id} /> </div> ); }}优化渲染性能若自定义开发成本较高,可集成第三方虚拟列表库(如 react-virtualized、react-window),需重点关注以下方面:
以 react-window 为例的集成思路:
通过上述方法,可显著提升 Ant Design 3.x Table 处理大数据量的渲染性能,具体方案需根据项目需求和技术栈灵活选择。