The React Native team has been working on something which is fundamentally going to change how the internals of React Native communication work with the host Operating System. It is nicely codenamed “project fabric” (until there’s an official name released).
React Native团队一直在进行一些工作,这些工作将从根本上改变React Native通信内部与主机操作系统的工作方式。 它很好地代号为“ project fabric”(直到正式名称发布)。
Let us discuss what it actually is and what changes it brings to you as a developer.
让我们讨论一下它的真正含义以及它为开发人员带来的变化。
If we take a look, React Native right now uses 3 threads:
如果我们看一下,React Native现在使用3个线程:
Let’s start from the beginning. Suppose you want to draw a red box in the center of your screen. So what happens is that your JS thread contains code to create a layout, i.e. that red box on the screen. Here’s a typical code snippet which might achieve that for React Native (RN):
让我们从头开始。 假设您要在屏幕中央绘制一个红色框。 因此,发生的事情是您的JS线程包含用于创建布局的代码,即屏幕上的红色框。 这是一个典型的代码片段,可以为React Native(RN)实现:
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<View style={{width: 100, height: 100, backgroundColor: "red"}}></View>
</View>
The host operating system has its own layout implementation and does not follow the kind of flexbox code you just wrote. Therefore, RN first of all has to convert your flexbox coded layout into a layout system which your host operating system can understand.
主机操作系统具有其自己的布局实现,并且不遵循您刚刚编写的那种flexbox代码。 因此,RN首先必须将您的flexbox编码布局转换为主机操作系统可以理解的布局系统。
Hold on! Before doing that, we need to offload this layout calculation part to another thread so we can keep executing our JavaScript thread. Hence, RN uses the Shadow Thread which essentially constructs a tree of your layout you coded in your JS thread. In this thread, RN uses a layout engine called Yoga which converts the flexbox-based layout into a layout system which your native host can understand.
坚持,稍等! 在此之前,我们需要将该布局计算部分卸载到另一个线程,以便我们可以继续执行我们JavaScript线程。 因此,RN使用了影子线程,该线程本质上构造了您在JS线程中编码的布局树。 在此线程中,RN使用了名为Yoga的布局引擎,该引擎将基于flexbox的布局转换为本地主机可以理解的布局系统。
React Native uses something called a React Native bridge to communicate this information from the JS thread to the Shadow thread. In a nutshell, this simply serializes the data in JSON format and transfers it as a string over the bridge.
React Native使用一种称为React Native的桥将这些信息从JS线程传递到Shadow线程。 简而言之,这只是简单地以JSON格式序列化数据并将其作为字符串通过桥传输。
At this point, we’re in the Shadow thread. The JS thread is executing and there’s nothing drawn on the screen.
至此,我们进入了影子线程。 JS线程正在执行,并且屏幕上没有任何内容。
Now, once we have the rendered markup from yoga, this information is again transferred to UI thread via the React Native bridge. Again, this does some serialization on the Shadow thread and deserialization on the main thread. Here, the main thread then renders the UI.
现在,一旦从瑜伽中获得了渲染的标记,该信息就会再次通过React Native桥传输到UI线程。 同样,这会对Shadow线程进行一些序列化,并对主线程进行反序列化。 然后,主线程在此处呈现UI。
If you see, all the communication among threads happens over a bridge which works, but is full of limitations. These include:
如果看到的话,线程之间的所有通信都是通过一个有效的桥进行的,但是充满了局限性。 这些包括:
Next, all communication is asynchronous, which in most cases is good. However, there is no way currently to update the UI thread from the JS thread synchronously. This creates a problem when you’re using, say, FlatList with a huge list of data. (You can think of FlatList as a weaker implementation of RecyclerView.)
接下来,所有通信都是异步的,这在大多数情况下是好的。 但是,当前无法同步从JS线程更新UI线程。 当您使用带有大量数据的FlatList时,这会产生问题。 (您可以将FlatList视为RecyclerView的较弱实现。)
Finally, due to this asynchronous nature of communication between the JS thread and UI thread, native modules which strictly require synchronous data access cannot be used to a complete extent. For example, RecyclerView’s Adapter on android requires synchronous access to the data it is rendering for not having flickers on screen. This is not possible right now due to the multi-threaded architecture setup by React Native.
最后,由于JS线程和UI线程之间的这种异步通信特性,严格要求同步数据访问的本机模块无法完全使用。 例如,Android上的RecyclerView适配器需要同步访问它正在渲染的数据,因为屏幕上不会出现闪烁。 由于React Native设置了多线程架构,因此目前无法实现。
Take a step back and think about your browser. If you take a deeper look, the input fields, the buttons, etc. are actually Operating System-specific. Therefore, it is your browser which asks your OS (Windows, Mac, Linux, or pretty much anything else) to draw, for example, an input field somewhere on a webpage. Holy moly! See the beautiful mapping from browsers to React Native.
退后一步,思考一下您的浏览器。 如果您进行更深入的研究,输入字段,按钮等实际上是特定于操作系统的。 因此,是您的浏览器要求您的OS(Windows,Mac,Linux或几乎其他任何设备)绘制例如网页上某处的输入字段。 天哪! 查看从浏览器到React Native的漂亮映射。
We know that modern browsers are very mature and handle all these tasks efficiently. So why not React Native? What is the missing piece of the puzzle which brutally restricts React Native but not browsers?
我们知道,现代浏览器非常成熟,可以高效地处理所有这些任务。 那么为什么不使用React Native? 残酷地限制了React Native但没有限制浏览器的难题的缺失部分是什么?
Have you ever just written commands like document.getElementById
and commands like setTimeout
and setInterval
in your console and seen the output? Oh! Their implementation is actually [native code]
! What does that mean?
您是否曾经在控制台中编写过诸如document.getElementById
类的命令以及诸如setTimeout
和setInterval
类的命令,并看到了输出? 哦! 它们的实现实际上是[native code]
! 那是什么意思?
You see, when you execute these functions, they do not call any JavaScript code. Instead, these functions are linked directly to native C++ code which is called. So the browser does not let JS communicate with the host Operating System using bridging, but instead, directly exposes JS to the OS using native code! In a nutshell, this is what React Native Fabric would do: eliminate the bridge and let the UI be controlled directly from the JS thread using native code.
您会看到,执行这些函数时,它们不会调用任何JavaScript代码。 相反,这些函数直接链接到被调用的本地C ++代码。 因此,浏览器不允许JS使用桥接与主机操作系统进行通信,而是使用本机代码将JS直接公开给OS! 简而言之,这就是React Native Fabric的工作方式:消除桥接,并使用本机代码直接从JS线程控制UI。
Image sources: https://www.slideshare.net/axemclion/react-native-fabric-review20180725
图片来源: https : //www.slideshare.net/axemclion/react-native-fabric-review20180725
If you liked this article, feel free to give me some claps and connect with me on twitter. You know the best part? Claps and twitter both are free! If you have any questions, feel free to drop them in the comments!
如果您喜欢这篇文章,请随时给我鼓掌,并在twitter上与我联系。 你知道最好的部分吗? 拍手和推特都是免费的! 如有任何疑问,请随时在评论中添加!
Quick shameless plug: If you’re getting started with React Native, here’s my 95% off course on how to get started with it: React Native — The First Steps
快速无耻的插件: 如果您要开始使用React Native,这是我入门的95%课程: React Native —第一步