# use-scan-detection-react18

> A react hook for detecting barcode scanner input.

Latest version **0.2.3** (published 2023-06-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-scan-detection-react18
pnpm add use-scan-detection-react18
yarn add use-scan-detection-react18
bun add use-scan-detection-react18
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.3 |
| Published | 2023-06-16 |
| First published | 2023-06-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 20.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 36 |
| Author | Mark Janiczak |
| Maintainers | mojtabafs |
| Keywords | barcode, upc, ean, scan |

## Links

- npm: https://www.npmjs.com/package/use-scan-detection-react18
- Repository: https://github.com/markjaniczak/use-scan-detection
- Homepage: https://github.com/markjaniczak/use-scan-detection#readme
- Issues: https://github.com/markjaniczak/use-scan-detection/issues
- npm.io page: https://npm.io/package/use-scan-detection-react18

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 0.2.3 (latest) — 2023-06-16

## README

# useScanDetection

![npm](https://img.shields.io/npm/v/use-scan-detection)
![downloads](https://img.shields.io/npm/dm/use-scan-detection)

A react hook for detecting barcode scanners in the DOM.

## Installation

```
npm i use-scan-detection
```

## Usage

```
useScanDetection({
    onComplete: (code) => { console.log(code) }
});
```

## Parameters

Parameters are supplied by a config object:

|   Parameter   |   Description |
|   ---         |   ---         |
|   averageWaitTime    | Average time between characters in milliseconds. Used to determine if input is from keyboard or a scanner. Defaults to `50`. |
| timeToEvaluate    | Time to evaluate the buffer after each character. |
| startCharacter | **Optional**. Character that barcode scanner prefixes input with. Buffer only starts if this character is read first. |
| endCharacter | **Optional**. Character that barcode scanner suffixes input with. Buffer is evaluated early if this character is read. Defaults to line return and escape. |
| onComplete | Function that is called when a complete barcode is scanned. Function is called with a single string which is the complete code. |
| onError | **Optional**. Function that is called when an incomplete barcode is scanned. Function is called with a single string which is the incomplete code. |
| minLength | Minimum number of characters for a barcode to successfully read. Should be greater than 0. Defaults to `1`. |
| ignoreIfFocusOn | **Optional**. DOM element that if focused prevents codes from being read. |
| stopPropagation | Whether to call stopPropagation on each key event. Defaults to `false`. |
| preventDefault | Whether to call preventDefault on each key event. Defaults to `false`. |
| container | DOM element to listen for keydown events in. Defaults to `document`.

## Return
This hook returns nothing.

## Example
```js
import React, { useState } from 'react';
import useScanDetection from 'use-scan-detection';

const Input = () => {
    const [value, setValue] = useState("");

    useScanDetection({
        onComplete: setValue,
        minLength: 13 // EAN13
    });

    return (
        <input 
            value={value} 
            type="text"
        />
    );
};

export default Input
```

## License
MIT Licensed

---
_Source: https://npm.io/package/use-scan-detection-react18 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
