如何解决React - 使用 `requestAnimationFrame` 调度状态更新以优化性能
This demo 有一个包含 1000 个项目的可排序列表。每个项目都可以拖放到列表中的新位置。 Sub openExcelDirect()
Dim objExcel As New Excel.Application
Dim WB As Excel.Workbook,SH1 As Excel.Worksheet,SH2 As Excel.Worksheet
Dim LastRow1 As Long,LastRow2 As Long,Array1 As Excel.Range,Array2 As Excel.Range
Dim Result1,Result2,LookupValue1,LookupValue2
Set WB = objExcel.Workbooks.Open("C:\Users\WB.xlsx")
objExcel.Visible = True 'this allows you to see the newly created session window
'you may comment this line when everything runs smooth
Set SH1 = WB.Worksheets("Sheet1")
Set SH2 = WB.Worksheets("Sheet2")
LastRow1 = SH1.Range("A" & SH1.Rows.Count).End(xlUp).Row
LastRow2 = SH2.Range("A" & SH2.Rows.Count).End(xlUp).Row
Set Array1 = SH1.Range(SH1.Cells(1,1),SH1.Cells(LastRow1,2)) 'well qualified range
Set Array2 = SH2.Range(SH2.Cells(1,SH2.Cells(LastRow1,4)) 'well qualified range
LookupValue1 = Me.ComboBox1
LookupValue2 = Me.ComboBox2
Result1 = SH1.Application.WorksheetFunction.VLookup(LookupValue1,Array1,2,False)
Result2 = SH2.Application.WorksheetFunction.VLookup(LookupValue2,Array2,False)
'Debug.Print Result1,Result2
ThisDocument.TextBox2 = Result1
ThisDocument.TextBox10 = Result2
WB.Close
End Sub
库促进了拖放操作,该库使用 HTML5 拖放 API 作为后端。
列表的顺序存储在状态变量 react-dnd
中(参见 state.cardsByIndex
的第 45 行)。当用户将项目 X 拖到列表上时,会调用 Container.jsx
函数以使用项目的当前索引更新 moveCard
(参见 state.cardsByIndex
的第 30 行)。但是,示例的作者没有直接调用 Container.jsx
,而是在 setState
回调中调用它作为优化措施:
requestAnimationFrame
我读过 scheduleUpdate(updateFn) {
this.pendingUpdateFn = updateFn;
if (!this.requestedFrame) {
this.requestedFrame = requestAnimationFrame(this.drawFrame);
}
}
drawFrame = () => {
const nextState = update(this.state,this.pendingUpdateFn);
this.setState(nextState);
this.pendingUpdateFn = undefined;
this.requestedFrame = undefined;
};
用于创建更多 efficient animations and prevent layout thrashing,但我没有使用它的任何经验,我不确定它如何在此示例中优化性能。
我看到该示例仅在没有排队的情况下(即,如果 rAF
为 setState
)调度 requestedFrame
调用来限制状态更新。但我不明白为什么需要 undefined
。它是否与浏览器的 rAF
重新渲染同步?这对我来说没有意义,因为 React 不保证在调用 setState
后立即应用状态更改。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。