Native modules let your React Native app use features that are only available in the phone's system. This helps your app do more things by talking directly to the phone's special parts.
Native modules concept in React Native
// Example of a native module in JavaScript import { NativeModules } from 'react-native'; const { MyNativeModule } = NativeModules; MyNativeModule.myMethod('Hello', (result) => { console.log(result); });
You import native modules from NativeModules in React Native.
Native modules are written in native code (Java/Kotlin for Android, Objective-C/Swift for iOS) and linked to JavaScript.
// Calling a native method
MyNativeModule.showToast('Hi!');// Getting a value from native code MyNativeModule.getDeviceName((name) => { console.log('Device name is ' + name); });
This React Native app has a button. When you press it, it calls a native module method to get a welcome message from the phone's native code. Then it shows that message on the screen.
import React from 'react'; import { View, Button, NativeModules, Text } from 'react-native'; export default function App() { const [message, setMessage] = React.useState(''); const showNativeMessage = () => { NativeModules.MyNativeModule.getWelcomeMessage((msg) => { setMessage(msg); }); }; return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Button title="Get Native Message" onPress={showNativeMessage} /> <Text>{message}</Text> </View> ); }
Native modules require writing code in native languages and linking it to React Native.
Always test native modules on real devices or emulators because they depend on platform features.
Use native modules only when React Native does not provide the needed feature.
Native modules connect React Native apps to phone features not available in JavaScript.
They are written in native code and called from JavaScript.
Use them to access special hardware or improve performance.
Practice
Solution
Step 1: Understand React Native limitations
JavaScript in React Native cannot access all device hardware or OS features directly.Step 2: Role of native modules
Native modules are written in native code to bridge this gap and provide access to those features.Final Answer:
To access device features not available in JavaScript -> Option AQuick Check:
Native modules = access device features [OK]
- Thinking native modules are for styling
- Confusing native modules with navigation libraries
- Believing native modules are UI components
CameraModule in React Native?Solution
Step 1: Recall native module import pattern
Native modules are accessed via the NativeModules object from 'react-native'.Step 2: Correct destructuring syntax
We import NativeModules, then extract CameraModule from it using const { CameraModule } = NativeModules.Final Answer:
import { NativeModules } from 'react-native'; const { CameraModule } = NativeModules; -> Option BQuick Check:
NativeModules destructuring = correct import [OK]
- Importing native module directly from 'react-native'
- Using require without NativeModules
- Wrong import source string
getBatteryLevel returns 85 asynchronously?
import { NativeModules } from 'react-native';
const { BatteryModule } = NativeModules;
BatteryModule.getBatteryLevel().then(level => {
console.log('Battery level is: ' + level);
});Solution
Step 1: Understand asynchronous method call
getBatteryLevel returns a Promise that resolves to 85.Step 2: Analyze the .then callback
The callback receives the resolved value 85 and logs 'Battery level is: 85'.Final Answer:
Battery level is: 85 -> Option DQuick Check:
Promise resolves to 85, logged correctly [OK]
- Logging the Promise object instead of resolved value
- Assuming synchronous return
- Confusing undefined with error
import { NativeModules } from 'react-native';
const { LocationModule } = NativeModules;
const location = LocationModule.getCurrentLocation();
console.log(location);Solution
Step 1: Identify asynchronous method usage
Native module methods like getCurrentLocation usually return Promises.Step 2: Check synchronous usage
The code assigns the Promise to location and logs it immediately, not waiting for resolution.Final Answer:
getCurrentLocation likely returns a Promise but is used synchronously -> Option AQuick Check:
Async method used without await or .then [OK]
- Ignoring Promise nature of native methods
- Trying to destructure NativeModules incorrectly
- Assuming console.log can't print objects
Solution
Step 1: Develop native code and register module
You must write native code (Java/Swift/Obj-C) to control flashlight and register it with React Native.Step 2: Import and call from JavaScript
Import NativeModules, extract your flashlight module, and call its methods asynchronously.Final Answer:
Write native code for flashlight, register module, import NativeModules, call methods asynchronously -> Option CQuick Check:
Native code + registration + async calls = correct usage [OK]
- Skipping native code or registration
- Calling native methods synchronously
- Assuming React Native UI covers hardware access
Ready to prove it?
Pass the test and this pattern moves unknown
