UNPKG

react-qrcode

Version:

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

25 lines • 903 B
import QRCode from 'qrcode'; import { useEffect, useState } from 'react'; export const isQRCodeValue = (valueOrOptions) => typeof valueOrOptions === 'string' || Array.isArray(valueOrOptions); export const useQRCode = (valueOrOptions) => { const [dataURL, setDataURL] = useState(); useEffect(() => { const isValue = isQRCodeValue(valueOrOptions); const value = (isValue ? valueOrOptions : valueOrOptions.value); let options; if (!isValue) { const { quality, ...props } = valueOrOptions; options = Object.assign(props, { renderOptions: { quality, }, }); } if (!value) { return setDataURL(''); } QRCode.toDataURL(value, options).then(setDataURL); }, [valueOrOptions]); return dataURL; }; //# sourceMappingURL=use-qrcode.js.map