9951 explained code solutions for 126 technologies


reactjsHow can I use ReactJS to create a QR reader?


ReactJS can be used to create a QR reader by using the react-qr-reader library. This library provides a React component that can be used to read QR codes from the camera. The following example code shows how to use the library:

import React from 'react'
import QrReader from 'react-qr-reader'

class MyQRReader extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      result: 'No result'
    }
  }

  handleScan = data => {
    if (data) {
      this.setState({
        result: data
      })
    }
  }

  handleError = err => {
    console.error(err)
  }

  render() {
    return (
      <div>
        <QrReader
          delay={300}
          onError={this.handleError}
          onScan={this.handleScan}
          style={{ width: '100%' }}
        />
        <p>{this.state.result}</p>
      </div>
    )
  }
}

This code creates a component that uses the QrReader component from the library. The delay prop is used to set the delay between scans in milliseconds. The onError and onScan props are used to handle errors and scan results respectively. The style prop is used to set the size of the component. When a QR code is scanned, the result is stored in the result state and displayed in a <p> tag.

Parts of the code:

  • import React from 'react': imports the React library.
  • import QrReader from 'react-qr-reader': imports the QrReader component from the react-qr-reader library.
  • constructor(props) { ... }: creates a constructor function that sets the initial result state to 'No result'.
  • handleScan = data => { ... }: creates a function that updates the result state with the data from a successful scan.
  • handleError = err => { ... }: creates a function that handles errors from the scanner.
  • <QrReader ... />: creates a QrReader component with the given props.
  • <p>{this.state.result}</p>: displays the result state in a <p> tag.

Helpful links

Edit this code on GitHub