React Native is a powerful framework for building mobile applications that run on both iOS and Android platforms using JavaScript and React. However, there are times when you need to access platform-specific features or use native APIs that are not available out of the box in React Native. This is where native modules come into play. Native modules allow you to extend the capabilities of React Native by writing custom native code in Java (for Android) or Objective-C/Swift (for iOS) and exposing it to your JavaScript code.

Using native modules in React Native involves a few key steps: creating the native module, bridging it to JavaScript, and using it within your React Native application. Let's dive into each of these steps in detail.

Creating a Native Module

To create a native module, you need to write platform-specific code. Let's start with Android:

Android

1. Create a Java Class: In your Android project, navigate to the android/app/src/main/java/com/yourappname directory and create a new Java class. This class will define your native module.

package com.yourappname;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;

public class MyNativeModule extends ReactContextBaseJavaModule {

    public MyNativeModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "MyNativeModule";
    }

    @ReactMethod
    public void getDeviceName(Promise promise) {
        try {
            String deviceName = android.os.Build.MODEL;
            promise.resolve(deviceName);
        } catch (Exception e) {
            promise.reject("Error", e);
        }
    }
}

2. Register the Module: You need to register your module with React Native. Create a new class that extends ReactPackage and override the necessary methods.

package com.yourappname;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyPackage implements ReactPackage {

    @Override
    public List createNativeModules(ReactApplicationContext reactContext) {
        List modules = new ArrayList<>();
        modules.add(new MyNativeModule(reactContext));
        return modules;
    }

    @Override
    public List createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }
}

3. Update the Application Class: Finally, register your package in the application class.

import com.yourappname.MyPackage;

@Override
protected List getPackages() {
    return Arrays.asList(
        new MainReactPackage(),
        new MyPackage() // Add this line
    );
}

iOS

1. Create a Swift/Objective-C File: In your iOS project, create a new Swift or Objective-C file. This file will define your native module.

// MyNativeModule.m

#import 

@interface RCT_EXTERN_MODULE(MyNativeModule, NSObject)

RCT_EXTERN_METHOD(getDeviceName:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)

@end

2. Implement the Module: Implement the module in Swift or Objective-C.

// MyNativeModule.swift

import Foundation

@objc(MyNativeModule)
class MyNativeModule: NSObject {

  @objc
  func getDeviceName(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
    let deviceName = UIDevice.current.name
    resolve(deviceName)
  }
}

3. Register the Module: Ensure that your module is registered in the AppDelegate.m or AppDelegate.swift file.

#import 

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // other setup code...
  return YES;
}

@end

Bridging Native Modules to JavaScript

Once you have created your native module, you need to bridge it to JavaScript so that you can use it in your React Native application. This involves creating a JavaScript module that calls the native methods.

import { NativeModules } from 'react-native';

const { MyNativeModule } = NativeModules;

export default MyNativeModule;

Now, you can use the native module in your React Native components:

import React, { useEffect, useState } from 'react';
import { View, Text } from 'react-native';
import MyNativeModule from './MyNativeModule';

const App = () => {
  const [deviceName, setDeviceName] = useState('');

  useEffect(() => {
    MyNativeModule.getDeviceName()
      .then(name => setDeviceName(name))
      .catch(error => console.error(error));
  }, []);

  return (
    
      Device Name: {deviceName}
    
  );
};

export default App;

Benefits and Use Cases of Native Modules

Native modules are particularly useful in scenarios where you need to:

  • Access Platform-Specific APIs: Some device features or APIs are only available natively, such as accessing the device's file system, camera, or sensors.
  • Integrate Third-Party Native Libraries: If you want to use a third-party library that is not available in JavaScript, you can create a native module to wrap the library's functionality.
  • Optimize Performance: Native code can offer performance benefits in certain situations, such as handling complex computations or graphics rendering.

Challenges and Considerations

While native modules provide powerful capabilities, they also come with some challenges:

  • Platform-Specific Code: Writing native modules requires knowledge of platform-specific programming languages (Java, Swift, Objective-C), which may increase the complexity of your project.
  • Maintenance: Keeping your native modules up-to-date with changes in the React Native framework and mobile operating systems can require ongoing maintenance.
  • Testing: Testing native modules can be more complex than testing pure JavaScript code, as it involves testing both the native and JavaScript layers.

Conclusion

Using native modules in React Native is a powerful way to extend the capabilities of your mobile applications. By writing custom native code, you can access platform-specific features, integrate third-party libraries, and optimize performance. However, it's important to weigh the benefits against the challenges and consider the long-term maintenance and testing implications. With the right approach, native modules can greatly enhance the functionality and user experience of your React Native applications.

Now answer the exercise about the content:

What is the primary purpose of using native modules in React Native applications?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Using Native Modules for Extending React Native Features: Understanding the Need for Native Modules

Next page of the Free Ebook:

12Using Native Modules for Extending React Native Features: Understanding the Need for Native Modules

6 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text