What if you could tap into every phone feature without rewriting your app twice?
Why Native modules concept in React Native? - Purpose & Use Cases
Imagine you want your React Native app to use the phone's camera or access special sensors. Without native modules, you'd have to write separate code for Android and iOS, then somehow connect it to your JavaScript app.
This manual way is slow and confusing. You might write the same logic twice, make mistakes, or struggle to keep both versions working together. It feels like juggling two different languages and hoping they talk well.
Native modules let you write the special phone features once in native code and easily call them from your React Native JavaScript. This way, your app can use powerful device features smoothly without messy work.
Write Java code for Android and Swift for iOS separately; then try to connect with JS manually.
Create a native module once and call it directly from React Native JavaScript.
It makes your app feel truly native by unlocking device features while keeping your code clean and easy to manage.
For example, a fitness app uses native modules to access the phone's step counter sensor, giving users real-time step tracking without complicated code.
Manual device feature access is slow and error-prone.
Native modules bridge native code and React Native smoothly.
This unlocks powerful device features with simple JavaScript calls.
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
