1.0.2 • Published 2 years ago

use-barcode-detection v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

useBarcodeDetection

React hook for detecting barcodes.

This hook wraps the BarcodeDetection API which has (very) limited support. If you know or can control what browser your users use this might be for you, but if not it's not recommended for use in production. Be sure to check the browser compatability table.

Installation

npm i use-barcode-detection

Usage

import useBarcodeDetection from "use-barcode-detection";

const ScannerComponent = () => {
  const [isScanning, setIsScanning] = useState(false);
  const onDetected = (barcodes: string[]) => {
    // Handle barcode detection...
    console.log(barcodes);

    // Deactivate scanning, maybe close a modal...
    setIsScanning(false);
  };
  const { ref } = useBarcodeDetection({
    interval: 150,
    active: isScanning,
    onDetected,
  });

  return (
    <>
      <video ref={ref} autoPlay playsInline muted />
      <button onClick={() => setIsScanning(!isScanning)}>
        {isScanning ? "Stop scanning" : "Start scanning"}
      </button>
    </>
  );
};

TODO

  • Better error handling
  • Add API reference
  • Tests
  • ...
1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

0.0.0

2 years ago