React QR Reader

3.0.0-beta-1 · active · verified Sun Apr 19

react-qr-reader is a React component designed for scanning QR codes directly from a user's webcam within a web browser application. Currently in `3.0.0-beta-1`, it is actively developed and compatible with React versions 16.8.0 and higher due to its internal use of React Hooks. The library leverages `jsQR` for QR code detection and `webrtc-adapter` for camera access, ensuring broad browser compatibility across Chrome, Firefox, and Safari on various operating systems including macOS, Android, and iOS. Key features include support for different camera facing modes, customizable scan delays, and a prop-driven API for integration into React applications. It primarily serves use cases requiring real-time QR code scanning in web environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate the QrReader component into a React functional component, capture scanned QR code data using state, and handle potential errors during scanning. It also shows how to configure camera constraints and styling.

import React, { useState } from 'react';
import { QrReader } from 'react-qr-reader';

const MyQrScanner = () => {
  const [scanResult, setScanResult] = useState('No result');

  return (
    <div style={{ width: '50%', margin: '0 auto' }}>
      <h2>QR Code Scanner</h2>
      <QrReader
        onResult={(result, error) => {
          if (!!result) {
            setScanResult(result?.text);
          }

          if (!!error) {
            // console.info(error); // Uncomment for debugging errors
          }
        }}
        constraints={{ facingMode: 'environment' }} // Prefer rear camera
        scanDelay={300} // Reduce delay for faster scans
        videoContainerStyle={{ padding: '20px', border: '1px solid #ddd' }}
        videoStyle={{ maxHeight: '300px' }}
      />
      <p style={{ marginTop: '20px', fontWeight: 'bold' }}>Scanned Data: {scanResult}</p>
      {scanResult === 'No result' && <p>Point your camera at a QR code.</p>}
    </div>
  );
};

export default MyQrScanner;

view raw JSON →