Native modules let your React Native app do things that JavaScript alone cannot. They connect your app to device features and native code for more power.
Why native modules extend capabilities in React Native
import { NativeModules } from 'react-native'; const { MyNativeModule } = NativeModules; MyNativeModule.someMethod(args, (error, result) => { if (error) { // handle error } else { // use result } });
You import native modules from NativeModules in React Native.
Native modules expose methods you can call from JavaScript.
import { NativeModules } from 'react-native'; const { DeviceInfo } = NativeModules; DeviceInfo.getDeviceName((error, name) => { if (!error) { console.log(name); } });
import { NativeModules } from 'react-native'; const { BatteryStatus } = NativeModules; BatteryStatus.getBatteryLevel().then(level => { console.log('Battery level:', level); });
This app uses a native module to get the device name and shows it on screen. You can refresh the name by pressing the button.
import React, { useEffect, useState } from 'react'; import { View, Text, Button, NativeModules } from 'react-native'; const { DeviceInfo } = NativeModules; export default function App() { const [deviceName, setDeviceName] = useState('Unknown'); const fetchDeviceName = () => { DeviceInfo.getDeviceName((error, name) => { if (!error) { setDeviceName(name); } }); }; useEffect(() => { fetchDeviceName(); }, []); return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Device Name:</Text> <Text style={{ fontWeight: 'bold', fontSize: 20 }}>{deviceName}</Text> <Button title="Refresh" onPress={fetchDeviceName} /> </View> ); }
Native modules require writing some code in Java (Android) or Swift/Objective-C (iOS).
They help bridge React Native with native device features and libraries.
Always test native modules on real devices or emulators because they depend on platform APIs.
Native modules let React Native apps use device features not available in JavaScript.
They connect JavaScript code with native platform code for more power and flexibility.
Use native modules when you need hardware access, native libraries, or better performance.
Practice
Solution
Step 1: Understand React Native limitations
JavaScript in React Native cannot access all device hardware or native libraries directly.Step 2: Role of native modules
Native modules bridge JavaScript with native platform code to access those features.Final Answer:
To access device features not available in JavaScript -> Option DQuick Check:
Native modules = access device features [OK]
- Thinking native modules replace JavaScript code
- Believing native modules slow down the app
- Assuming native modules are optional for all features
Solution
Step 1: Recall React Native import syntax
NativeModules is a named export from 'react-native', so it must be imported with curly braces.Step 2: Check each option
import { NativeModules } from 'react-native'; uses correct syntax with curly braces; others are incorrect or invalid module names.Final Answer:
import { NativeModules } from 'react-native'; -> Option AQuick Check:
Correct import uses curly braces [OK]
- Omitting curly braces for named imports
- Using wrong module names
- Using require without assignment
import { NativeModules } from 'react-native';
const { DeviceInfo } = NativeModules;
DeviceInfo.getDeviceName((name) => {
console.log('Device:', name);
});Solution
Step 1: Understand native module callback
The native module 'DeviceInfo' calls the callback with the device name 'Pixel 5'.Step 2: Check console.log output
The callback logs 'Device: Pixel 5' to the console.Final Answer:
Device: Pixel 5 -> Option AQuick Check:
Callback logs device name correctly [OK]
- Assuming synchronous return instead of callback
- Expecting undefined if native module not linked
- Confusing function names or parameters
TypeError: NativeModules.CameraModule.getPicture is not a function. What is the most likely cause?Solution
Step 1: Analyze the error message
The error says the function does not exist on the native module object.Step 2: Understand native module linking
If the native module is not linked or registered correctly, its functions won't be available in JavaScript.Final Answer:
The native module is not properly linked or registered -> Option CQuick Check:
Linking issues cause missing native functions [OK]
- Assuming import errors cause this
- Ignoring native build steps
- Confusing variable names with linking problems
Solution
Step 1: Identify biometric sensor access needs
Biometric sensors require native platform APIs not accessible directly from JavaScript.Step 2: Choose the correct integration method
Native modules bridge JavaScript with native biometric APIs for secure and efficient access.Step 3: Evaluate other options
JavaScript alone or WebView cannot access biometric hardware securely or at all.Final Answer:
Use a native module to connect JavaScript with the platform's biometric API -> Option BQuick Check:
Native modules enable hardware access [OK]
- Trying to access hardware only with JavaScript
- Ignoring security and platform API requirements
- Using WebView for native hardware access
Ready to prove it?
Pass the test and this pattern moves unknown
