react中component存在性能问题
生活随笔
收集整理的這篇文章主要介紹了
react中component存在性能问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1). 父組件重新render(), 當前組件也會重新執行render(), 即使沒有任何變化
2). 當前組件setState(), 重新執行render(), 即使state沒有任何變化
1). 原因: 組件的componentShouldUpdate()默認返回true, 即使數據沒有變化render()都會重新執行
2). 辦法1: 重寫shouldComponentUpdate(), 判斷如果數據有變化返回true, 否則返回false
3). 辦法2: 使用PureComponent代替Component
4). 說明: 一般都使用PureComponent來優化組件性能
1). 重寫實現shouldComponentUpdate()
2). 對組件的新/舊state和props中的數據進行淺比較, 如果都沒有變化, 返回false, 否則返回true
3). 一旦componentShouldUpdate()返回false不再執行用于更新的render()
組件的哪個生命周期勾子能實現組件優化?
PureComponent的原理?
區別Component與PureComponent?
轉載于:https://www.cnblogs.com/korea/p/11186830.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的react中component存在性能问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [jQuery]点击某元素之外触发事件
- 下一篇: 标识符注释关键字