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.mzph.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;都是正整数。 解集不能包含重复的…