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.
npm install react-qr-readerVerified import paths — ran on the pinned version, not inferred.
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.
Replace `deviceId` prop with `constraints` prop, specifying `facingMode: 'user'` for front camera or `facingMode: 'environment'` for rear camera. Example: `<QrReader constraints={{ facingMode: 'environment' }} />`.Upgrade your project's `react` and `react-dom` dependencies to `^16.8.0` or higher. Ensure your `package.json` reflects these peer dependencies.
Ensure your application is served over HTTPS when deploying to production. For local development, `localhost` is usually treated as a secure context.
Implement robust error handling in the `onResult` prop to gracefully inform users if their device or browser does not support webcam access. Consider providing a fallback mechanism, like uploading an image, if QR scanning isn't possible.
Update `react-qr-reader` to the latest stable version to benefit from performance improvements in QR code detection.
Ensure your `react` and `react-dom` dependencies are `^16.8.0` or higher. Also, make sure the component is rendered within a proper React functional component context.
Instruct users to grant camera permissions. For production, ensure your application is deployed over HTTPS. For local development, `localhost` is typically allowed.
Add a null-check for `result` before accessing its properties within the `onResult` callback: `if (!!result) { setData(result.text); }`Review the `constraints` prop. The requested camera (front/rear) might not be available or the device doesn't support the specified resolution/aspect ratio. Consider more generic `constraints` or provide user options to select cameras.