-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathHtml5QrcodePlugin.jsx
50 lines (43 loc) · 1.43 KB
/
Html5QrcodePlugin.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Html5QrcodeScanner } from 'html5-qrcode';
import { useEffect } from 'react';
const qrcodeRegionId = "html5qr-code-full-region";
// Creates the configuration object for Html5QrcodeScanner.
const createConfig = (props) => {
let config = {};
if (props.fps) {
config.fps = props.fps;
}
if (props.qrbox) {
config.qrbox = props.qrbox;
}
if (props.aspectRatio) {
config.aspectRatio = props.aspectRatio;
}
if (props.disableFlip !== undefined) {
config.disableFlip = props.disableFlip;
}
return config;
};
const Html5QrcodePlugin = (props) => {
useEffect(() => {
// when component mounts
const config = createConfig(props);
const verbose = props.verbose === true;
// Suceess callback is required.
if (!(props.qrCodeSuccessCallback)) {
throw "qrCodeSuccessCallback is required callback.";
}
const html5QrcodeScanner = new Html5QrcodeScanner(qrcodeRegionId, config, verbose);
html5QrcodeScanner.render(props.qrCodeSuccessCallback, props.qrCodeErrorCallback);
// cleanup function when component will unmount
return () => {
html5QrcodeScanner.clear().catch(error => {
console.error("Failed to clear html5QrcodeScanner. ", error);
});
};
}, []);
return (
<div id={qrcodeRegionId} />
);
};
export default Html5QrcodePlugin;