CodeBlanks Logo Icon CodeBlanks

What are native modules?

Learn the native module concept.

What is a native module?

A native module is a bridge that exposes platform functions (camera, sensor, file system) inaccessible from JavaScript to JS. It is written in Objective-C/Swift (iOS) or Java/Kotlin (Android); React Native automatically exposes it as an object on the JS side. It is accessed via NativeModules.

Example: the device's battery level, Bluetooth state or a custom third-party SDK come as native modules. JS calls them as if they were ordinary objects. Behind the scenes the bridge relays the call to the UI thread and returns the result to JS. This way React Native goes beyond its limits.

Access via NativeModules

React Native exports NativeModules from the react-native package. This object carries all registered native modules. You import it with import { NativeModules } from 'react-native'; then access it like NativeModules.BatteryLevel.

The module name is given during native registration; the same name is used in JS. Calls are usually asynchronous; because they run on the UI thread and JS cannot return immediately. They are awaited or used as promises.

Practice Import NativeModules

Complete the empty file. Import NativeModules from react-native. Export it with export { NativeModules }.

Starter code

App.js
// import and export NativeModules from react-native

Sample solution

App.js
import { NativeModules } from 'react-native';

export { NativeModules };

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does a native module provide?

  • Only animation
  • Only defining styles
  • Accessing platform functions from JS Correct answer
  • Only network requests

Explanation: A native module provides access to platform functions from JS.

2 Import NativeModules

Question
import { ____ } from 'react-native';
Answer
import { NativeModules } from 'react-native';

Explanation: NativeModules carries all native modules.

3 Match each part to its role

Term Meaning
NativeModules Carries all modules
NativeModules.Battery Accesses one module
Objective-C/Swift iOS side
Java/Kotlin Android side

All lessons in Advanced