Друкарня від WE.UA

Why Does React Native Use a Bridge and How Has the New Architecture Changed It?

React Native allows you to develop mobile applications that can work on Android and iOS. The application is programmed in JavaScript. However, the application needs to access the phone's native functionality. The question that will come up early on from anyone trying to learn this technology, perhaps by taking a React Native Course, is: How does the JavaScript communicate with the native code, written in Java, Kotlin, Objective-C, and Swift?" Well, for many years, the answer was an additional component known as the Bridge. The Bridge transferred messages between the JavaScript code and the native code. And this was working well for many years. However, as applications became larger and busier, the Bridge started showing its weaknesses.

Why Does React Native Need a Bridge?

JavaScript and native code exist in two different worlds in React Native. JavaScript is responsible for the app’s logic. This includes the screen, state, and the rules that the application should follow. The native code performs platform-specific actions. This involves the camera, sensors, push notifications, and native buttons or menus.
The two components do not communicate with each other directly. This requires an intermediary component. This intermediary component is known as the bridge. It received requests from the JavaScript part and forwarded them to the native code. After completing its work, the native code returned the result via the Bridge. After finishing its job, the native code sent back the result through the Bridge.

How Did the Traditional React Native Bridge Handle Communication?

The Bridge would go through the same process every single time there was a request from JavaScript for the native code.

  • Sending a Request from JavaScript to the Native Module: A request from JavaScript would be sent to a native module. This means that an application that wants to get the battery status of the phone, for example, would make such a request using the Bridge.

  • Serialisation and Transmission of the Message: The Bridge would serialise the message and transmit it in JSON format. There were many requests made at once in order to reduce the number of trips.

  • Native Results Returning to JavaScript: After the completion of the native code, it sent the result or an event back. It did so through the same Bridge but in a format that JavaScript could easily utilise.

This system worked well for small and medium applications. Problems appeared mainly in large applications.

What Limitations Did the Traditional Bridge Create?

The conversion process from data to text and vice versa required additional time. This aspect was especially important when significant amounts of data were transferred between JavaScript and native code. Due to the batch nature of messages and their transfer in the background, delays were possible. It affected processes that required frequent and fast interaction between both parties.

Animations and rapid screen updates were the most affected aspects. Trainees of the React Native Certification Course encounter this issue quite frequently when developing these kinds of features. However, the Bridge was not the only cause of application slowness. Still, its architecture created a restriction.

How Has React Native’s New Architecture Changed JavaScript-Native Communication?

The New Architecture does not just improve on the old Bridge. It changes the entire process of communication between JavaScript and native code.

1. Introducing JSI for More Direct Communication: JSI means JavaScript Interface. It allows JavaScript to call native C++ code directly. There is no need for every call to be wrapped in JSON. This allows for reducing most of the overhead that was required by the old Bridge. Some calls can now be executed immediately without waiting for the background process.

2. Utilising TurboModules to Enhance Native Modules Loading: TurboModules use JSI to load native code on-demand. In other words, TurboModules are loaded lazily. The app starts up faster because unused code is not loaded at all. Native capabilities become more easily accessible and more reliable.

3. Introducing Fabric for Modern UI Rendering: Fabric is the new rendering engine that draws the screens of the application. It represents a new way of communication between JavaScript and native screen elements. It aligns the React rendering process with the native one better. It also makes possible the immediate execution of some layout tasks.

4. Using Codegen to Connect JavaScript and Native Components: Codegen generates native code by itself. This is done based on rules typed for TurboModules and Fabric. The benefit here is that there is less coding done manually. Also, the JavaScript and native sides are synchronised with each other to prevent errors.

Overall, all these four components give React Native a faster and more direct way of communicating between JavaScript and native code.

What Changes Do Developers Experience with the New Architecture?

A simple table can demonstrate changes in the developers' work.

Aspect

Traditional Bridge

New Architecture

Communication

Slower and in JSON format

Direct, through JSI

Loading native code

Loading all together upon app initialisation

Loaded only when needed

Drawing screens

Separated from JavaScript thread

Matched closely through Fabric

Writing interfaces

Done manually

Done automatically by Codegen

Individuals who take React Native Training in Delhi always wonder if it is relevant to their work life. The real answer is that it is highly dependent on the app. Applications that have a lot of animation or live data see the most benefit, while simple applications might not see any change at all.

Conclusion

The Bridge was originally used by React Native as a way of integrating JavaScript with native code. This model performed well for many years. However, with the growth of applications, the slow text-based approach turned into a bottleneck. The New Architecture solves the problem using JSI, TurboModules, Fabric, and Codegen. Every component solves one of the problems with the old model. Communication between JavaScript and native code has become more straightforward. The loading of native code happens only when necessary. Screens have been better integrated with JavaScript. It does not solve all the performance issues of an application.

Статті про вітчизняний бізнес та цікавих людей:

Поділись своїми ідеями в новій публікації.
Ми чекаємо саме на твій довгочит!
Kirtika Sharma
Kirtika Sharma@J_Lcmou_2U5JMI0

31Довгочити
641Перегляди
На Друкарні з 25 травня

Більше від автора

Це також може зацікавити:

Коментарі (0)

Підтримайте автора першим.
Напишіть коментар!

Це також може зацікавити: