0

In React Native it is possible to bring native functionality from Android and iOS in multiple ways. I always thought that all possible ways were limited by platform-related languages like Java/Kotlin and Objective-C/Swift. However, I noticed that it is still possible to bridge native functionality even from C++ (without using JSI). Specifically, I noticed that from react-native-builder-bob it is possible to easily start a package that bridges native modules using C++.

At this point I wonder, but what does JSI introduce that is new if it was already possible to integrate JS with C++? Why should it bring performance improvements over the current solution?

I apologise in advance for my lack of knowledge, but I really couldn't find the answer.

4

1 回答 1

1

Native 和 JS 之间的当前React Native Bridge架构是异步工作的,并且仅以 JSON 格式传输数据。

它产生下一个问题:

异步调用

  • 许多线程和它们之间的跳转:JS、Shadow、Main、Native...
  • JS 和主线程不直接通信(UI 渲染慢)

JSON

  • JS 和 Native 线程之间没有数据共享
  • 由于 JSON 序列化,数据传输缓慢(瓶颈)

您可以连接到任何本机代码 Java/Konlin、ObjC/Swift、C++ 等,但您总是会遇到上述问题。

React Native JSI为 JS 运行时引擎提供 API,并允许直接向 JS 公开本机函数和对象 - 根本没有桥接器。

它提供了以下优势:

  • 将 JS 线程的调用同步到 Native,反之亦然
  • 使用直接调用 UI 主线程进行快速渲染
  • 线程间数据共享

您必须仅使用 C++ 来处理 JSI,因为 JS Runtime 具有 C++ API,但可以在 JSI 和您现有的 Java 或 Swift 代码之间创建 C++ 层。

JSI 是未来新的 React Native 架构的基础,其中包括:Fabric、TurboModules、CodeGen。阅读更多:https ://github.com/react-native-community/discussions-and-proposals/issues/91

于 2021-12-16T14:18:16.810 回答