首先逃魄,需要檢查出為什么速度慢
自定義時間工具,標記檢測時間
function watchTime() {
let quie: { falgStr: any; time: number }[] = []
return (falgStr?: string, status = '', falgTimeList: string[] = [], isRest = false) => {
if (status === 'one' && quie.find(v => v.falgStr === falgStr)) {
return false
}
if (isRest) {
quie = []
}
quie.push({
falgStr: falgStr,
time: new Date().getTime()
})
if (quie.length === 1) {
const first = quie[0]
console.log(`watchTime--------------------------***-------- 0 :`, first.time, ' 標記:', falgStr)
} else if (quie.length > 1) {
const first = quie[0]
const prev = quie[quie.length - 2]
const current = quie[quie.length - 1]
console.log(`watchTime--------------------------***-------- ${current.time} `, ' 標記:', falgStr)
console.log(`距離上一個間隔時間${current.time - prev.time}`)
console.log(`距離第一個間隔時間${current.time - first.time}`)
if (falgTimeList && falgTimeList.length && Array.isArray(falgTimeList)) {
falgTimeList.forEach(v => {
const index = quie.findIndex(v2 => v2.falgStr === v)
if (index !== -1) {
const falgQ = quie[index]
console.log(`距離${falgQ.falgStr} 間隔時間${current.time - falgQ.time}`)
}
})
}
}
}
}
const logWatchTime = watchTime()
const api = new Promise((resolve) => setTimeout(() => {resolve()}, 1000))
export defualt function(){
logWatchTime('init-detail-fn', 'one', [], true)
const onRefresh = () => {
logWatchTime('onRefresh-before')
api.then(() => {
logWatchTime('onRefresh')
setState()
setState()
setState()
logWatchTime('onRefresh-02', '', ['onRefresh-before'])
setState()
setState()
setState()
logWatchTime('onRefresh-end')
})
}
}
這里可用清晰檢測到卸耘, 多個useState后退敦, 明細速度會慢很多, 因此蚣抗, 把多個useState合并成一個侈百, 可用讓這中間的速度給增快很多。