Sitemap

React Native: Dig Deeper

5 min readOct 4, 2022

--

Press enter or click to view image in full size
Excavating basic concepts of React Native

In mid 2017, I fractured my right ankle while on a hike in Hong Kong and that put me in bed for 3 months. I was searching for new technologies that I could learn during this time, something in the mobile space as that always fascinated me. I knew a bit about Android development, but had no practical experience. Another criteria was to be able to learn something quick and that’s when I landed on React Native. It was still very new but I was confident that it would be a game changer because Facebook built it and companies like Airbnb, Netflix were also using it.

I purchased some React Native tutorials, and in 4 months I had the confidence to build something. That’s when I started my first company (which is a story for another day).

React Native has evolved sooo much since then and it’s truly magical as I found my calling there. But there are always some basic topics in React Native that some developers choose to ignore (or it could just be me). So I’ll explain a few basic things about React Native. These are the topics that I’ll be covering:

  1. Metro
  2. Flipper
  3. Hermes
  4. Fabric (to be continued)
  5. Native APIs (to be continued)
  6. turbomodules (to be continued)
  7. codegen (to be continued)
  8. JSI (to be continued)

Let’s begin

Metro Bundler

Metro is a JavaScript bundler that takes in an entry file, along with various options, and compiles everything into a single JavaScript file that includes all your code and it’s dependencies. When you start a React Native project by running `react-native start`, metro gets to work and you see “BUNDLE” written in a green box in the image below which shows that files are being bundled every time you run a new project or reload a current one.
I would strongly recommend reading its documentation

Press enter or click to view image in full size
Metro bundler

Job description of a Metro bundler

  • Make hot-reload possible with sub-second reload cycles, making it easier to debug app UIs without having to wait for the app to restart.
  • Accelerate startup to the app on the device/emulator by providing quick bundling speed.
  • Combine all JavaScript code into a single file and transpile any code the device won’t understand, such as JSX.
  • Convert assets (e.g. PNG files) into objects that can be displayed by an Image component.

Some of the most important features that provide React Native developers a good development experience are due to Metro Bundler. (Woot woot!!)

Flipper

I’m amazed by this debugging tool, which is a must for my development stack. So how does Flipper makes my life easier?

  • Direct Debugging
    Remote debugging means when execution of JS code is assigned to a different engine( Chrome V8) instead of using JavascriptCore (which is the default RN JS engine). Running JS code in an engine, which will not be used by the users is a big risk. (I struggled for 2 days to figure out what was going wrong in my code as it was giving out the right data in a debugger but failing when debugging was turned off).
  • Network inspector
    It gives me the ability to inspect all network/api calls made by the app.
  • Metro and simulator actions
    It’s easy to perform metro and simulator commands from Flipper. Usually, to reload the app from the simulator, I have to use Cmd+R. Sometimes this fails, so I have to open the debugger and reload from there. Flipper provides a reload button.
  • Crash reporter
    It’s easier to inspect and understand crashes, thanks to Flipper
  • Plugins
    Plugins make Flipper a winning debugger, as I can create my own. It’s pretty neat to be able to modify and adapt for my needs. My favs are
    - React Native performance link
    - Shared Preferences
    -
    Reactotron-in-Flipper
  • React DevTool and Live Editing
    Just like I’m able to do on web, I can change fonts and colors without having to refresh the app
Press enter or click to view image in full size
Live editing

There is another debugger that I really like is: Reactotron (there is a plugin available in flipper Reactotron-in-Flipper)

Hermes

Hermes is an open source JavaScript engine designed to optimize performance by reducing app launch time and precompiling JavaScript into efficient bytecode.

Get Simran’s stories in your inbox

Join Medium for free to get updates from this writer.

Hermes contributes a considerable reduction in bundle size, load time, and consumption size with a GUI to understand the performance metrics of your application during development. This feature is helpful to learn how applications will perform in production.

Hermes is the most performant JavaScript engine for building React Native applications (according to research conducted by maintainers of React Native). The research was conducted on three metrics that developers deem important: Time to Interactive (TTI), binary size, and consumption size.

  • Time to Interactive (TTI) is the duration between when the app was launched and when a user can interact with it
  • Binary size is the size of the bundled React Native application in APK (Android) or APA (iOS)
  • Memory consumption is the size of the application after it’s installed on a device.

Link to the article about the research

Building an application with Hermes is easy to configure.

For ios- add this code inside your podfile

use_react_native!(
:path => config[:reactNativePath],
:hermes_enabled => true
)

Next, install pods:

$ cd ios && pod install

That’s it! You should now be able to develop and deploy your app as usual:

$ npx react-native run-ios

For android- if you have recently created an app, it should be enabled. But, to check if it’s enabled, HermesInternal global variable is available to verify that Hermes is in use:

const isHermes = () => !!global.HermesInternal;

If hermes is not enabled in Android

Edit your android/app/build.gradle file and make the change illustrated below:

project.ext.react = [
entryFile: "index.js",
- enableHermes: false // clean and rebuild if changing
+ enableHermes: true // clean and rebuild if changing
]

Also, if you’re using ProGuard, you will need to add these rules in proguard-rules.pro :

-keep class com.facebook.hermes.unicode.** { *; }
-keep class com.facebook.jni.** { *; }

Next, if you’ve already built your app at least once, clean the build:

$ cd android && ./gradlew clean

That’s it! You should now be able to develop and deploy your app as usual:

$ npx react-native run-android

This is a two part series of React Native basics that everyone should know. I really hope the details above gave you an insight into the power of the React Native ecosystem.

A big thanks to Derek Greenberg and Gant Laborde for helping me write my first post on React Native. It would not have been possible without them.

--

--