Add a camera barcode scanner to your React app in one line
Every "scan a barcode in the browser" tutorial ends up re-implementing the same fiddly lifecycle: ask for the camera, draw frames, decode them, debounce duplicates, and release the camera when the component unmounts (mis
Every "scan a barcode in the browser" tutorial ends up re-implementing the same fiddly lifecycle: ask for the camera, draw frames, decode them, debounce duplicates, and release the camera when the component unmounts (miss that last one and the camera light stays on after the user navigates away).
So we packaged it. @barqscan/react gives you a component and a hook.
npm install @barqscan/react @barqscan/web react
import { BarqScanner } from "@barqscan/react";
export function Checkout() {
return (
<BarqScanner
licenseKey="BARQ-xxxx" // free on localhost, no signup
scanner={{ symbologies: ["ean13", "code128", "qr"] }}
style={{ width: "100%", maxWidth: 480, aspectRatio: "3 / 4" }}
onScan={(e) => console.log(e.barcodes[0].symbology, e.barcodes[0].data)}
/>
);
}
That's the whole integration. It requests the back camera, renders the preview, decodes on-device with WebAssembly (nothing is uploaded), and calls onScan for every confirmed read. It's StrictMode-safe and tears the camera down on unmount.
Need your own UI? Use the headless hook:
const { ref, status, error, scanner } = useBarqScanner({
licenseKey: "BARQ-xxxx",
onScan: (e) => console.log(e.barcodes),
});
// attach ref to any <div>, read status: idle | starting | scanning | error | blocked
Vue 3 has the same API: @barqscan/vue.
It's free on localhost, so you can have it scanning in your dev app in a minute without an account. Full React guide: https://docs.barqscan.com/guide/react.html
(Disclosure: I build BarqScan - a web-first, on-device barcode SDK and a cheaper alternative to enterprise scanners.)
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.