A React Component for read QR codes from the camera
npm install react-qr-scanRepository: https://github.com/ssttechdev/react-qr-scan
- Use Case
- Compatibility
- Installation
- NPM
- YARN
- Example Usage
- QrReader API
- Browser support
- Issues
- Contributing
- License
You need a component for Scanning QR codes from a web browser based app.
This component has been tested in the following browsers:
- Chrome Mac OS & Android
- Firefox Mac OS & Android
- Safari Mac OS & IOS
Since this library does internal use of hooks you need React >= 18.0.0 (React 19 supported).
You can install this library via NPM or YARN.
``bash`
npm i react-qr-scan
`bash`
yarn add react-qr-scan
After reading and performing the previous steps, you should be able to import the library and use it like in this example:
`javascript
import React, { useState } from 'react';
import { QrReader } from 'react-qr-scan';
const Test = (props) => {
const [data, setData] = useState('No result');
return (
<>
result,
error,
codeReader,
location,
locationError,
deviceInfo
) => {
if (!!result) {
setData(result?.text);
}
if (!!error) {
console.info(error);
}
if (!!location) {
console.info(
'Location:',
location.coords.latitude,
location.coords.longitude
);
}
if (!!locationError) {
console.info('Location error:', locationError);
}
if (!!deviceInfo) {
console.info('Device info:', deviceInfo);
}
}}
enableLocation
enableDeviceInfo
style={{ width: '100%' }}
/>
{data}
Notes
- Geolocation requires user permission and only works on HTTPS or localhost.
-
enableDeviceInfo is heuristic and can be spoofed by the browser.Component API
The
QrReader component has the following props:| Properties | Types | Default Value | Description |
| ------------------- | ----------------------------------------------------------------------------------------------- | ------------------------ | -------------------------------------------------------- |
| constraints | MediaTrackConstraints |
{ facingMode: 'user' } | Specify which camera should be used (if available). |
| onResult | function | none | Scan event handler |
| videoId | string | video | The ID for the video element |
| scanDelay | number | 500 | The scan period for the QR hook |
| enableLocation | boolean | false | Attach geolocation data to scan results |
| locationOptions | PositionOptions | none | Options for the geolocation API |
| enableDeviceInfo | boolean | false` | Attach device info (heuristic) to scan results |- Created by @JodusNodus .
- Revived thanks to @JonatanSalas and his company @BlackBoxVision .
- Updated by @wsnugrho
If you need to support older browsers, checkout this guide in how to make it compatible with legacy ones
Please, open an issue following one of the issues templates. We will do our best to fix them.
If you want to contribute to this project see contributing for more information.
Distributed under the MIT license. See LICENSE for more information.