Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated both example.js and README to a hook based approach (fix #30)… #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;