Skip to content

Instantly share code, notes, and snippets.

@maruware
Last active August 14, 2019 07:30
Show Gist options
  • Save maruware/2fd634a2757c1fec0e11e3634e024029 to your computer and use it in GitHub Desktop.
Save maruware/2fd634a2757c1fec0e11e3634e024029 to your computer and use it in GitHub Desktop.
React QRCode Element
import React, { useState, useEffect } from 'react'
import QRCode from 'qrcode'
export interface QRCodeImageProps {
value: string
}
const QRCodeImage: React.FC<
QRCodeImageProps & React.ImgHTMLAttributes<HTMLImageElement>
> = ({ value, ...rest }) => {
const [dataUrl, setDataUrl] = useState('')
const options: QRCode.QRCodeToDataURLOptions = {}
if (rest.width) {
options.width = parseInt(rest.width.toString())
}
useEffect(() => {
QRCode.toDataURL(value, options).then(url => setDataUrl(url))
}, [value])
return <img src={dataUrl} {...rest} />
}
export default QRCodeImage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment