To build a QR code scanner in React Native, you need to integrate a camera library with QR decoding capability—most commonly using react-native-vision-camera or react-native-camera—and pair it with a QR decoder like react-native-qrcode-scanner or zxing-js. This creates a native mobile app that accesses the device camera, detects QR codes in real time, and returns their decoded content. You’ll need a working React Native environment (React Native CLI or Expo), basic JavaScript/TypeScript knowledge, and appropriate platform permissions.
What You’ll Need
- Node.js v18+ and npm or yarn installed
- React Native CLI (for bare React Native) or Expo Go (for managed Expo workflow)
- Physical iOS or Android test device (emulators may lack camera support or require extra setup)
- Basic understanding of React hooks and native module permissions
- Access to iOS Simulator or Android Emulator with virtual camera support (optional, for limited testing)
Step-by-Step
- Initialize a new React Native project
Runnpx react-native init QrScannerApp(for bare React Native) ornpx create-expo-app qr-scanner(for Expo). Navigate into the project folder withcd QrScannerApp. - Install required libraries
For Expo, runnpm install react-native-camera react-native-qrcode-scanner(note: Expo’s managed workflow requires usingexpo installfor native modules, or switch to bare workflow for full control). For bare React Native, use the same install command. Alternatively, for better performance and modern support, usenpm install react-native-vision-camera @react-native-camera-roll/camera-rolland pair withzxing-jsvianpm install @zxing/library. - Configure iOS permissions
Openios/QrScannerApp/Info.plist(orios/Info.plistin Expo). Add<key>NSCameraUsageDescription</key><string>This app needs camera access to scan QR codes</string>. If usingreact-native-camera, also add<key>NSPhotoLibraryUsageDescription</key><string>This app needs photo library access to save scanned content</string>. - Configure Android permissions
Openandroid/app/src/main/AndroidManifest.xml. Inside the<manifest>tag, add<uses-permission android:name="android.permission.CAMERA" />. Inside the<application>tag, ensure<uses-feature android:name="android.hardware.camera" android:required="false" />is present. For Android 10+, also add<uses-permission android:name="android.permission.FLASHLIGHT" />if you plan to control the torch. - Create the scanner component
InApp.js, import camera hooks. Forreact-native-qrcode-scanner, use:import React, { useState, useRef } from 'react';
import { View, Text, StyleSheet, Alert } from 'react-native';
import QRCodeScanner from 'react-native-qrcode-scanner';
import { RNCamera } from 'react-native-camera';
export default function App() {
const => {
Alert.alert('QR Code Scanned', e.data);
};
return (
<QRCodeScanner
> flashMode={RNCamera.Constants.FlashMode.off}
topContent={<Text style={styles.centerText}>Scan a QR code</Text>} bottomContent={null}
/> );
}
Forreact-native-vision-camera, use the frame processor API withZXing for detection—but this requires more manual setup (see Troubleshooting). - Handle permissions at runtime
In Expo, useexpo-cameraand callawait Camera.requestCameraPermissionsAsync()before rendering the scanner. In bare React Native withreact-native-camera, you can use therequestCameraPermissionsmethod on the camera instance or rely on the scanner component’s built-in handling (most do this automatically). - Test with a real QR code
Generate a test QR code using a free online generator (e.g., qr-code-generator.com). Point your device camera at it—the scanner should vibrate (if supported) and display the decoded value via theonSuccesscallback.
Common Mistakes
- Forgetting to add platform-specific permissions in
Info.plistorAndroidManifest.xml—the app will crash silently or show a black screen. - Using outdated packages like
react-native-cameraon React Native 0.60+ without linking (manual linking is required in bare React Native; Expo handles it automatically). - Testing only in an emulator without enabling virtual camera support (e.g., Android Emulator requires a virtual device with camera hardware feature enabled).
- Not handling camera permissions gracefully—users may deny access, so always wrap permission calls in try/catch and show fallback UI.
- Assuming all QR codes are scannable at any distance or angle—ensure your component includes zoom controls or visual guides for best results.
Tips
- Use
react-native-qrcode-scannerfor rapid prototyping—it includes built-in torch toggle, auto-focus, and vibration feedback. - For production apps, prefer
react-native-vision-camerawithzxing-jsfor better performance and control over frame processing. - Add a
torchModeprop to toggle flashlight when scanning low-light QR codes (e.g.,torchMode={RNCamera.Constants.TorchMode.on}). - Style the scanner with a custom overlay (e.g., corner borders) using the
cameraOverlayprop to guide users. - On Android, disable the screen timeout during scanning with
PowerManager.WakeLockif scanning takes longer than 30 seconds.
Troubleshooting
- App crashes on launch with “Camera permission denied”
VerifyNSCameraUsageDescriptionexists inInfo.plist(iOS) orAndroidManifest.xml(Android). In Expo, ensure you’re not using an older SDK version that requiresexpo-cameraexplicitly. - Scanner shows black screen but no error
Check if the device has a working camera—try the native camera app. In emulators, select a device with camera hardware (e.g., Pixel 4 with front/back camera support). - QR codes not detected despite clear visibility
Ensure the QR code is well-lit and fills at least 50% of the camera frame. AdjustautoFocusandzoomprops if available. Withreact-native-vision-camera, add a frame processor that logs detected codes to debug decoding. - Build fails with “duplicate class” errors
This often happens when mixingreact-native-cameraandreact-native-vision-camerain the same project. Remove unused packages and runcd android && ./gradlew cleanbefore rebuilding. - Torch does not turn on
ConfirmhasFlashis true viacamera.getParameters().hasFlash()(Android) orRNCamera.Constants.FlashMode(iOS). Some devices (e.g., older iPads) lack flash hardware.
Frequently Asked Questions
Can I use React Native Web for QR scanning?
No—camera access requires native device hardware. QR scanning only works on iOS and Android; web browsers lack direct camera API access in React Native Web.Does Expo support QR scanning out of the box?
Yes, but only in the managed workflow withexpo-camera or react-native-camera via expo install. For full control, you may need to eject to bare React Native.How do I save the scanned QR content?
InonSuccess, store e.data in state, write to AsyncStorage, or send to a backend API. For iOS, use Clipboard.setString(e.data) to copy to clipboard if needed.Can I scan QR codes from an image file instead of camera?
Yes—usereact-native-qrcode-scanner’s showMarker and cameraType props to switch to back or front, or use react-native-image-picker to select an image and decode it with zxing-js manually.Why does my scanner work on Android but not iOS?
iOS requires explicit permission descriptions inInfo.plist. Also, ensure your iOS deployment target is ≥ 10.0 and you’re not using an outdated react-native-camera version incompatible with newer Xcode.What if the QR code contains binary data?
Most decoders return the raw string. For binary (e.g., vCards), parse the string manually or use libraries likeqrcode-parser for structured formats.