react-barcode-scanner
v4.1.0
Published
A barcode scanner base on Barcode Detector
Downloads
44,754
Readme
Introduction
A lightweight barcode scanner based on the Barcode Detection API, with a zbar.wasm polyfill.
Usage
import { BarcodeScanner } from 'react-barcode-scanner'
import 'react-barcode-scanner/polyfill'
export default () => {
return <BarcodeScanner />
}State scope and multiple scanners
Without a Provider, the scanner and state hooks use a shared compatibility scope, so existing single-scanner usage continues to work.
Use a separate BarcodeScannerProvider for each active scanner when controls or status live in other components:
import {
BarcodeScanner,
BarcodeScannerProvider,
useStreamState,
useTorch
} from 'react-barcode-scanner'
function ScannerControls () {
const [stream] = useStreamState()
const { isTorchSupported, isTorchOn, setIsTorchOn } = useTorch()
return (
<button
disabled={!stream || !isTorchSupported}
onClick={() => setIsTorchOn(!isTorchOn)}
>
Torch: {isTorchOn ? 'on' : 'off'}
</button>
)
}
export default function ScannerPanel () {
return (
<BarcodeScannerProvider>
<BarcodeScanner />
<ScannerControls />
</BarcodeScannerProvider>
)
}See State Scope and Multiple Scanners for default values, lifecycle, torch operations and SSR guidance.
Detail
License
MIT
