關於React性能優化

這幾天陸陸續續看了一些關於React性能優化的博客,大部分提到的都是React 15.3新加入的PureComponent ,經過使用這個類來減小React的重複渲染,從而提高頁面的性能。使用過React的朋友都知道,React組件只有在state和props發生改變時纔會觸發render,若是state和props沒有發生改變,render就不執行。一般在寫頁面的時候,若是沒有使用PureComponent類,爲了不重複渲染而產生的性能問題,咱們會使用shouldComponentUpdate手動來比較頁面是否須要從新渲染,這時咱們是能夠進行深比較的,也就是將當前的state與nextState或者props與nextProps一層一層進行比較,若是比較發現不相等,這個函數就返回true從而使組件從新渲染,若是比較後發現值沒有改變則返回false來阻止組件從新渲染。當咱們使用了PureComponent類後,就再也不須要手動檢查組件是否須要從新渲染了,由於PureComponent會幫咱們檢查state和prop是否發生改變,從而決定是否要調用render方法,從而提升性能。可是須要注意的是,使用PureComponent時,React只是作了最外層的淺比較:html

if (this._compositeType === CompositeTypes.PureClass) {
shouldUpdate = !shallowEqual(prevProps, nextProps) || ! shallowEqual(inst.state, nextState);
}
shadowEqual只會淺檢查組件的props和state,因此嵌套對象和數組是不會被比較的。因此使用了PureComponent的時候,咱們要特別注意這一點。web

好了,今天要分享的除了上面的關於PureComponent的原理外,還要分享一篇關於React性能優化的乾貨博客,數組

英文原文: React Performance Fixes on Airbnb Listing Pages:https://medium.com/airbnb-engineering/recent-web-performance-fixes-on-airbnb-listing-pages-6cd8d93df6f4性能優化

譯文:Airbnb 愛彼迎房源詳情頁中的 React 性能優化:https://juejin.im/entry/5bab390c5188255c8a06013f函數

轉載地址:https://www.f2ecoder.net/857.html性能

相關文章
相關標籤/搜索