Last active
August 14, 2019 07:30
-
-
Save maruware/2fd634a2757c1fec0e11e3634e024029 to your computer and use it in GitHub Desktop.
React QRCode Element
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
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