forked from kybarg/react-qr-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated both example.js and README to a hook based approach (fix kyba…
- Loading branch information
1 parent
fd1f4fd
commit 8944be9
Showing
2 changed files
with
56 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,32 @@ | ||
import React, { Component } from 'react' | ||
import QrReader from 'react-qr-scanner' | ||
import React, { useState } from "react"; | ||
import QrReader from "react-qr-scanner"; | ||
|
||
class Example extends Component { | ||
constructor(props){ | ||
super(props) | ||
this.state = { | ||
delay: 500, | ||
result: 'No result', | ||
} | ||
function Example() { | ||
const [codeRead, setCodeRead] = useState(""); | ||
|
||
this.handleScan = this.handleScan.bind(this) | ||
function onScan(result) { | ||
console.log(`Identified: ${result}`); | ||
setCodeRead(result); | ||
} | ||
handleScan(result){ | ||
if(result){ | ||
this.setState({ result }) | ||
} | ||
} | ||
handleError(err){ | ||
console.error(err) | ||
} | ||
render(){ | ||
const previewStyle = { | ||
height: 240, | ||
width: 320, | ||
} | ||
|
||
return( | ||
<div> | ||
<QrReader | ||
delay={this.state.delay} | ||
style={previewStyle} | ||
onError={this.handleError} | ||
onScan={this.handleScan} | ||
/> | ||
<p>{this.state.result}</p> | ||
</div> | ||
) | ||
function onError(error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
return ( | ||
<div> | ||
<QrReader | ||
delay={500} | ||
style={{ | ||
width: 320, | ||
height: 240, | ||
}} | ||
onError={onError} | ||
onScan={onScan} | ||
/> | ||
<p>{codeRead}</p> | ||
</div> | ||
); | ||
} | ||
|
||
export default Example; |