UNPKG

react-qrcode

Version:

🤳 A React component for QR code generation with `qrcode`

37 lines (31 loc) • 1.08 kB
import QRCode, { QRCodeToDataURLOptions } from 'qrcode' import { useEffect, useState } from 'react' import { QRCodeOptions, QRCodeValue, _QRCodeValue } from './types.js' export const isQRCodeValue = ( valueOrOptions: unknown, ): valueOrOptions is QRCodeValue => typeof valueOrOptions === 'string' || Array.isArray(valueOrOptions) export const useQRCode = (valueOrOptions: QRCodeOptions | QRCodeValue) => { const [dataURL, setDataURL] = useState<string>() useEffect(() => { const isValue = isQRCodeValue(valueOrOptions) const value = ( isValue ? valueOrOptions : valueOrOptions.value ) as _QRCodeValue let options: QRCodeToDataURLOptions | undefined if (!isValue) { const { quality, ...props } = valueOrOptions options = Object.assign(props, { renderOptions: { quality, }, }) } if (!value) { return setDataURL('') } // eslint-disable-next-line @typescript-eslint/no-floating-promises QRCode.toDataURL(value, options).then(setDataURL) }, [valueOrOptions]) return dataURL }