import React, { Component } from 'react'import BarcodeReader from 'react-barcode-reader' class Test extends Component { constructor(props){ super(props) this.state = { result: 'No result', } this.handleScan = this.handleScan.bind(this) } handleScan(data){ this.setState({ result: data, }) } handleError(err){ console.error(err) } render(){ return( <div> <BarcodeReader onError={this.handleError} onScan={this.handleScan} /> <p>{this.state.result}</p> </div> ) }}
function BarcodeScanner (props) {
const [barcodeInputValue, updateBarcodeInputValue] = useState('')
function barcodeAutoFocus() {
document.getElementById("SearchbyScanning").focus()
}
function onChangeBarcode(event) {
updateBarcodeInputValue(event.target.value)
}
function onKeyPressBarcode(event) {
if (event.keyCode === 13) {
updateBarcodeInputValue(event.target.value)
}
}
return () {
<div>
<input
autoFocus={true}
placeholder='Start Scanning'
value={barcodeInputValue}
onChange={onChangeBarcode}
id='SearchbyScanning'
className='SearchInput'
onKeyDown={onKeyPressBarcode}
onBlur={barcodeAutoFocus}
/>
</div>
}
}