Simple React Native Bridge Concept for Android

React Native bridge for Android

When using React Native to write mobile apps, JavaScript is the primary programming language. However, sometimes, you may want to call a platform API that is not supported by existing React Native component or use a third-party Android library for these cases, you need to write native code for bridging using Android Studio and Xcode. In this article, I will demonstrate how to create the simplest React Native Bridge module step by step.

Requirements to build Package for React Native Bridge

We are creating a bridge/package for React Native to access Toast (From Android Native) using JavaScript.

For creating any package first we need to setup the basic directory structure so, let us create the directory structure as below.

  1. Create Package Directory:

mkdir react-native-android-toast

  1. Run the following command to initiate the package:

cd react-native-android-toast
npm init

When you run the npm init it will ask you about the package. Enter all the information related to the package and it will create package.json file.

Now you have to create one android directory and one index.js file.

Once the basic directory is setup now we have to setup the android to write java/native code create the android directory structure as below

ToastModule.java:

ToastPackage.java

ToastPackage is used to register the module. Just copy the code into your ToastPackage.java file and change as per your requirements.

Index.js

This file is used to wrap the native module in a JavaScript module.

A NativeModule is a Java class that usually extends the ReactContextBaseJavaModule class and implements the functionality required by the JavaScript.

This is basically used to call our module from the javascript.

So far, we have successfully setup the Android and JavaScript communication bridge.

How to use this package into our Project?

Create a React Native project: react-native init Example.

Since all Node modules should be installed under {React Native project}\node_modules\.

Simply add your package directory name into the React Native Project’s package.json file and path of your Package and run the following command to use into your project.

npm install
react-native link

react-native link command will link your package into your project and you are ready to access the toast from javascript.

Now Just import that package into your JS file where you want to show the toast into your project.

Here, we are calling the Native Method from ReactNative.

ToastModule.show("Hello World");

That’s it! Run your project and use toast anywhere into your project.

Click here for more blogs


At BoTree Technologies, we build enterprise applications with our ReactNative team of 10+ engineers.

We also specialize in Ruby on Rails, Python, RPA, AI, Django, NodeJS and ReactJS.

Consulting is free – let us help you grow!

Related posts

Why should you use Django for Web Development

by Shardul Bhatt
5 years ago

Why choose Python for Web Development in 2023?

by Shardul Bhatt
4 years ago

What are some most Popular Java Frameworks for Software Development

by Parth Barot
4 years ago
Exit mobile version