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

  1. Initialize a new React Native project
    Run npx react-native init QrScannerApp (for bare React Native) or npx create-expo-app qr-scanner (for Expo). Navigate into the project folder with cd QrScannerApp.
  2. Install required libraries
    For Expo, run npm install react-native-camera react-native-qrcode-scanner (note: Expo’s managed workflow requires using expo install for 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, use npm install react-native-vision-camera @react-native-camera-roll/camera-roll and pair with zxing-js via npm install @zxing/library.
  3. Configure iOS permissions
    Open ios/QrScannerApp/Info.plist (or ios/Info.plist in Expo). Add <key>NSCameraUsageDescription</key><string>This app needs camera access to scan QR codes</string>. If using react-native-camera, also add <key>NSPhotoLibraryUsageDescription</key><string>This app needs photo library access to save scanned content</string>.
  4. Configure Android permissions
    Open android/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.
  5. Create the scanner component
    In App.js, import camera hooks. For react-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}
    /> );
    }

    For react-native-vision-camera, use the frame processor API withZXing for detection—but this requires more manual setup (see Troubleshooting).
  6. Handle permissions at runtime
    In Expo, use expo-camera and call await Camera.requestCameraPermissionsAsync() before rendering the scanner. In bare React Native with react-native-camera, you can use the requestCameraPermissions method on the camera instance or rely on the scanner component’s built-in handling (most do this automatically).
  7. 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 the onSuccess callback.

Common Mistakes

  • Forgetting to add platform-specific permissions in Info.plist or AndroidManifest.xml—the app will crash silently or show a black screen.
  • Using outdated packages like react-native-camera on 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-scanner for rapid prototyping—it includes built-in torch toggle, auto-focus, and vibration feedback.
  • For production apps, prefer react-native-vision-camera with zxing-js for better performance and control over frame processing.
  • Add a torchMode prop 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 cameraOverlay prop to guide users.
  • On Android, disable the screen timeout during scanning with PowerManager.WakeLock if scanning takes longer than 30 seconds.

Troubleshooting

  • App crashes on launch with “Camera permission denied”
    Verify NSCameraUsageDescription exists in Info.plist (iOS) or AndroidManifest.xml (Android). In Expo, ensure you’re not using an older SDK version that requires expo-camera explicitly.
  • 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. Adjust autoFocus and zoom props if available. With react-native-vision-camera, add a frame processor that logs detected codes to debug decoding.
  • Build fails with “duplicate class” errors
    This often happens when mixing react-native-camera and react-native-vision-camera in the same project. Remove unused packages and run cd android && ./gradlew clean before rebuilding.
  • Torch does not turn on
    Confirm hasFlash is true via camera.getParameters().hasFlash() (Android) or RNCamera.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 with expo-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?

In onSuccess, 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—use react-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 in Info.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 like qrcode-parser for structured formats.