react fiber_讓我們愛上React Fiber

react fiber

by Ryan Yurkanin

瑞安·尤卡寧(Ryan Yurkanin)

讓我們愛上React Fiber (Let’s fall in love with React Fiber)

TLDR, React Fiber is an internal engine change that allows React to break the limits of the call stack. It’s creation enables React to pause/start rendering work at will. Eventually, React users will be able to hint at the “priority” of work.

TLDR, React Fiber是一個內部引擎更改,允許React打破調用堆棧的限制。 它的創建使React可以隨意暫停/開始渲染工作。 最終,React用戶將能夠暗示工作的“優先級”。

Currently, we can’t directly interface with it, so why should we care about it? Because it’s really freaking cool!

當前,我們不能直接與它交互,那么為什么我們要關心它呢? 因為這真是太酷了!

在Fiber像沒有git的快節奏公司工作之前做出React。 (React before Fiber was like working at a fast paced company without git.)

Imagine being in the middle of a huge feature, and your boss needs a hotfix, pronto. You can’t stop working though because all your changes are in one file, you’re committed to finishing this work.

想象一下,在一項巨大功能的中間,而您的老板需要一個修補程序,即pronto。 但是,由于所有更改都在一個文件中,因此您無法停止工作,您將致力于完成這項工作。

If we were using git, we would be able to commit our work to a branch, and switch to a quick hotfix branch.

如果使用的是git,則可以將工作提交到分支,然后切換到快速修補程序分支。

With Fiber, React can pause and resume work at will to get working on what matters as soon as possible! ?

有了Fiber,React可以隨意暫停和恢復工作,以便盡快處理重要的事情!

簡而言之React內部構件? (React Internals in a nutshell ?)

You create a tree of components. React takes this tree, walks through it, and creates a virtual model of the end result. Perhaps you are rendering to the DOM, perhaps you are targeting native. At this point, that doesn’t matter to React.

您將創建一個組件樹。 React拿起這棵樹,遍歷它,并創建最終結果的虛擬模型。 也許您正在渲染到DOM,也許您正在針對本機。 在這一點上,這對React無關緊要。

Now, as your app updates, React will do that process of creating the virtual result over and over again. Each time, it compares the previous virtual tree to the next one.

現在,隨著您的應用程序更新,React將反復執行創建虛擬結果的過程。 每次,它都會將前一個虛擬樹與下一個虛擬樹進行比較。

At this point, we get platform-dependent. If you are rendering to the DOM, it could be that only one class on one element changed. React will walk through the virtual tree, find what’s changed, and update as little as it can.

至此,我們得到了平臺相關的信息。 如果要渲染到DOM,則可能是一個元素上只有一個類被更改。 React將遍歷虛擬樹,查找已更改的內容,并盡可能少地進行更新。

This could mean updating one class attribute, or it could mean tearing down the whole DOM. This is Reconciliation.

這可能意味著更新一個類屬性,或者可能意味著拆除整個DOM。 這就是和解 。

Before Fiber, this was it. The work was laid out, and the renderer of choice got to work. Even if the browser was lagging, the user was typing, or the planet was about to explode, the render train wouldn’t stop. ?

在出現Fibre之前就是這樣。 工作已經完成,選擇的渲染器開始工作。 即使瀏覽器滯后,用戶正在鍵入或星球即將爆炸,渲染火車也不會停止。 ?

它是如何工作的(高水平)? (How does it work (at a high level)?)

With Fiber, there are now varying levels of priority for updates. Updating an input a user is typing into has higher priority than a list with thousands of components.

使用光纖,現在有不同的更新優先級。 與具有數千個組件的列表相比,更新用戶輸入的輸入具有更高的優先級。

Fiber breaks tree computation into units of work that it can “commit” at any time. So what is a unit of work? It’s simply a node in your component tree!

Fibre將樹計算分解為可以隨時“提交”的工作單元。 那么什么是工作單元? 它只是組件樹中的一個節點!

  1. React can now pause, resume, and restart work on a component. This means certain lifecycle hooks may fire more than once.

    現在,React可以暫停,繼續和重新啟動組件上的工作。 這意味著某些生命周期掛鉤可能會觸發多次。
  2. React can have a priority-based update system. This allows the React Team to fine tune the renderer so that React is fastest during the most common use cases.

    React可以有一個基于優先級的更新系統。 這使React團隊可以微調渲染器,以便在最常見的用例中React最快。

I want to focus on that first point a bit, though. React is going to be moving away from (but still supporting!) some old lifecycle hooks, and adding some new ones! ?

不過,我想重點談一下第一點。 React將遠離(但仍在支持!)一些舊的生命周期掛鉤,并添加一些新的掛鉤! ?

componentWillMount, componentWillUpdate, componentWillReceiveProps, can now fire multiple times. You shouldn't trigger side effects here.

現在, componentWillMountcomponentWillUpdatecomponentWillReceiveProps可以觸發多次。 您不應該在這里觸發副作用。

Now, you want to fire side effects in the lifecycle hooks that will only fire once: componentDidMount and componentDidUpdate

現在,您要在僅觸發一次的生命周期掛鉤中觸發副作用: componentDidMountcomponentDidUpdate

To make up for a lot of the use cases that componentWillReceiveProps covered, we will be receiving two new hooks.

為了彌補componentWillReceiveProps涵蓋的許多用例,我們將收到兩個新的鉤子。

  1. getDerivedStateFromProps which doesn't have access to previous props or the component instance, but allows you to sync state with your props.

    getDerivedStateFromProps ,它無權訪問以前的道具或組件實例,但允許您將狀態與道具同步。

  2. getSnapshotBeforeUpdate gives you access to the DOM before it gets updated. The value you return is usable in componentDidUpdate.

    getSnapshotBeforeUpdate使您可以在更新DOM之前對其進行訪問。 您返回的值可在componentDidUpdate

As of React 16.4, getDerivedStateFromProps now always fires before the render method. Not just when props update!
從React 16.4開始,getDerivedStateFromProps現在總是在render方法之前觸發。 不只是道具更新時!

In summary, Fiber allows React to fine tune rendering to make sure the most important updates happen as soon as possible, all for the light cost of some lifecycle hooks, and gallons of Facebook dev blood. ?

總而言之, Fiber允許React進行微調渲染,以確保最重要的更新盡快發生,而這一切都是為了節省生命周期的鉤子以及Facebook開發人員的加侖成本。

If you have any questions or are looking for one-on-one React mentorship, feel free to tweet me @yurkaninryan any time!

如果您有任何疑問或正在尋找一對一的React指導,請隨時在@yurkaninryan上發消息給我!

If you like my writing style, here are some other articles that I’ve done.

如果您喜歡我的寫作風格,這是我做過的其他文章。

Good luck and happy coding! ??

祝您好運,編碼愉快! ??

翻譯自: https://www.freecodecamp.org/news/lets-fall-in-love-with-react-fiber-90f2e1f68ded/

react fiber

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393860.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393860.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393860.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Ajax爬取豆瓣電影目錄(Python)

下面的分析相當于一個框架,搞懂之后,對于類似的文字爬取,我們也可以實現。就算不能使用Ajax方法,我們也能夠使用相同思想去爬取我們想要的數據。 豆瓣電影排行榜分析 網址:https://movie.douban.com/explore#!typemovi…

到底死不死我就請了七天假_“你到底死不死?我只請了7天假”

這兩天看到一條令人心酸的新聞,在國內某地鐵站內,一位57歲的大媽突發心臟病,被緊急救醒后,第一句話竟是請求工作人員不要打電話通知她遠在德國的兒子。看完這條新聞,掌柜特別心酸,孤身一人在國內&#xff0…

正面管教PHP沙龍,正面管教沙龍體會

接觸到正面管教這個理念是我們南寧行動派伙伴圈 的圈主西西給大家帶來的分享,謝謝西西[愛你]圖片發自簡書App同時也很感謝親切溫柔,知性優雅的Liliane老師,讓我明白表揚和鼓勵的區別,非暴力教育……教書育人這個道路上我需要學習的…

FB面經Prepare: Dot Product

Conduct Dot Product of two large Vectors 1. two pointers 2. hashmap 3. 如果沒有額外空間,如果一個很大,一個很小,適合scan小的,并且在大的里面做binary search 1 package fb;2 3 public class DotProduct {4 5 publi…

leetcode1291. 順次數(回溯)

我們定義「順次數」為:每一位上的數字都比前一位上的數字大 1 的整數。 請你返回由 [low, high] 范圍內所有順次數組成的 有序 列表(從小到大排序)。 示例 1: 輸出:low 100, high 300 輸出:[123,234] …

20175223 MySQL

目錄 完成結果要求 1 :導入world.sql要求 2 :CityWanna.javaCityWanna.java要求 3 :CountryWanna.javaCountryWanna.java要求 4 :LifeWanna.javaLifeWanna.java過程中問題及解決1. XAMPP無法啟用 MySQL 程序。目錄 完成結果 要求 …

2020運動相機推薦_2020年超有價值入門級微單相機推薦,超高性價比幾款入門級微單相機(選購指南)...

學習攝影專業已經3年多啦,自己喜歡拍攝照片,自己還幫助過一些想學習攝影的朋友快速入門,最近發現周圍學習攝影的朋友也越來越多了,有一些朋友咨詢關于入門微單相機的問題,想讓推薦幾款不錯的入門的微單相機。這篇文章帶…

javascript入門_JavaScript代理快速入門

javascript入門What is a JavaScript proxy? you might ask. It is one of the features that shipped with ES6. Sadly, it seems not to be widely used.什么是JavaScript代理? 你可能會問。 這是ES6附帶的功能之一。 可悲的是,它似乎并未得到廣泛使用…

linux缺少文件操作數,linux 文件的atime,ctime,mtime查看與修改

查看ls -a默認顯示的是修改時間ls -c / --timestatus / --timectime顯示的是狀態修改時間(即權限修改時間)ls -u / --timeuse / --timeaccess / --timeatime表示的是文件訪問時間修改touch: 缺少了文件操作數請嘗試執行“touch --help”來獲取更多信息。[weilocalhost ~]$ touc…

leetcode47. 全排列 II(回溯)

給定一個可包含重復數字的序列&#xff0c;返回所有不重復的全排列。 示例: 輸入: [1,1,2] 輸出: [ [1,1,2], [1,2,1], [2,1,1] ] 代碼 class Solution {List<List<Integer>> cListnew ArrayList<>();public List<List<Integer>> permuteUni…

linux 磁盤查看方式

fdisk (查看物理磁盤大小) df (查看文件系統&#xff0c;也就是正在使用磁盤大小) lsblk (查看邏輯磁盤大小)轉載于:https://www.cnblogs.com/MUQINGFENG123/p/10820345.html

ioslabel陰影,UILabel的內陰影

is it possible to create such a UILabel with inner and outer shadow?i only know shadowColor and shadowOffsetzoomed:thanks!解決方案The answer by dmaclach is only suitable for shapes that can easily be inverted. My solution is a custom view that works with …

Webpack初學者介紹

Webpack is a tool that lets you compile JavaScript modules. It’s also known as a module bundler.Webpack是使您可以編譯JavaScript模塊的工具。 也稱為模塊捆綁器 。 Given a large number of files, it generates a single file (or a few files) that run your app.給…

Android Coding利器之掌握小技巧,助你Coding更上一層樓~

本文講的是Android Coding利器之掌握小技巧&#xff0c;助你Coding更上一層樓~&#xff0c;話說前幾天在網上瀏覽到一大牛寫的關于Android布局優化的文章&#xff0c;看后感觸很深&#xff0c;回過頭看看自己寫過的代碼&#xff0c;發現還是有不少需要改進&#xff0c;今天找不…

linux系統報警怎么辦,常見Linux系統故障和解決方法

常見Linux系統故障和解決方法發布時間&#xff1a;2020-06-06 14:48:19來源&#xff1a;億速云閱讀&#xff1a;212作者&#xff1a;Leah欄目&#xff1a;云計算這篇文章給大家分享的是常見的Linux系統故障和解決方法。在使用系統的過程中總會有各種各樣的故障&#xff0c;所以…

Vuex 模塊化與項目實例 (2.0)

Vuex 強調使用單一狀態樹&#xff0c;即在一個項目里只有一個 store&#xff0c;這個 store 集中管理了項目中所有的數據以及對數據的操作行為。但是這樣帶來的問題是 store 可能會非常臃腫龐大不易維護&#xff0c;所以就需要對狀態樹進行模塊化的拆分。 首先貼出一個邏輯比較…

click js自動點擊 vue_vue.js2.0點擊獲取自己的屬性和jquery方法

如下所示&#xff1a;:data-index"index":dt"index"v-on:click"onclick($event,index)":data-d "JSON.stringify( item)"href"http://www.baidu.com" rel"external nofollow" rel"external nofollow"da…

Python:知識目錄

Python目錄 第一篇&#xff1a;數據類型部分文件操作 基礎數據類型---str 基礎數據類型---List 基礎數據類型---dict 基礎數據類型---set 基礎數據類型---bytes 數據類型的總結 文件操作------讀&#xff0c;寫 文件操作------使用方法 第二章&#xff1a;函數模塊 初識函數…

初學者css常見問題_5分鐘內學習CSS-初學者教程

初學者css常見問題關于網絡設計語言的快速教程。 (A quick tutorial on the design language of the web.) CSS (Cascading Style Sheets) is what makes web pages look good and presentable. It’s an essential part of modern web development and a must-have skill for …

leetcode39. 組合總和(回溯)

給定一個無重復元素的數組 candidates 和一個目標數 target &#xff0c;找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重復被選取。 說明&#xff1a; 所有數字&#xff08;包括 target&#xff09;都是正整數。 解集不能包含重復的…