web前端全系列 教程
1839个小节阅读:2242.9k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
我们必须要明白的一个道理,js执行永远要比dom渲染快的多。 所以对于大量的数据,一次性渲染,容易造成卡顿,卡死的情况。
xxxxxxxxxx
import React, { Component } from 'react';
export default class LargeDom extends Component {
constructor(){
super()
this.state={list:[]}
}
componentDidMount(){
this.setState({list:new Array(40000).fill(0)})
}
render() {
return (
<div>
{this.state.list.map((item,index)=><li key={index}>
这是第{index+1}条数据
</li>)}
</div>
);
}
}
xxxxxxxxxx
import React, { Component } from 'react';
export default class LargeDom extends Component {
constructor(){
super()
this.state={list:[]}
}
componentDidMount(){
this.preList=new Array(40000).fill(0)//定义初始的40000条数据
this.sliceTime(0)
}
//时间分片,每一次渲染100条数据,渲染400次
sliceTime=(times)=>{
console.log('times',times)
if(times==400) {
return
}
setTimeout(()=>{
const sliceList=this.preList.slice(times*100,((times+1)*100))
this.setState({list:this.state.list.concat(sliceList)})
this.sliceTime(times+1)
},0)
}
render() {
return (
<div>
{this.state.list.map((item,index)=><li key={index}>
这是第{index+1}条数据
</li>)}
</div>
);
}
}