Skip to content

Commit

Permalink
Updated both example.js and README to a hook based approach (fix kyba…
Browse files Browse the repository at this point in the history
  • Loading branch information
pedropalhari committed Sep 23, 2020
1 parent fd1f4fd commit 8944be9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 72 deletions.
66 changes: 29 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,38 @@ Thanks to initial repo https://github.com/JodusNodus/react-qr-reader
## Example

```js
import React, { Component } from 'react'
import QrReader from 'react-qr-scanner'

class Test extends Component {
constructor(props){
super(props)
this.state = {
delay: 100,
result: 'No result',
}

this.handleScan = this.handleScan.bind(this)
}
handleScan(data){
this.setState({
result: data,
})
}
handleError(err){
console.error(err)
import React, { useState } from "react";
import QrReader from "react-qr-scanner";

function Example() {
const [codeRead, setCodeRead] = useState("");

function onScan(result) {
console.log(`Identified: ${result}`);
setCodeRead(result);
}
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;
```

## Props
Expand Down
62 changes: 27 additions & 35 deletions examples/example.js
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;

0 comments on commit 8944be9

Please sign in to comment.