import React, { useState, useEffect } from 'react';
import QRCode from 'qrcode';
import DownloadIcon from '@mui/icons-material/Download';
import { useTranslation } from "next-i18next"
const QRCodeGenerator = (props) => {
const { t } = useTranslation(["inventory"]);
const propsurl = props.url
const [qrCodeURL, setQRCodeURL] = useState(null);
const generateQRCode = async () => {
try {
const url = await QRCode.toDataURL(propsurl);
setQRCodeURL(url);
} catch (error) {
console.error('Failed to generate QR code', error);
}
};
useEffect(() => {
generateQRCode();
}, [propsurl]);
const QRDownloadHandler = () => {
if (qrCodeURL) {
const link = document.createElement('a');
link.href = qrCodeURL;
link.download = 'qrcode.png';
link.click();
}
};
return (
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', flexDirection: 'column' }}>
{qrCodeURL && <img src={qrCodeURL} alt="QR Code" style={{ marginBottom: '10px' }} />}
<button
onClick={QRDownloadHandler}
style={{ display: 'flex', alignItems: 'center', backgroundColor:'#000', color:'#fff',border: 'none', cursor: 'pointer', fontSize: '16px', padding:'8px 24px', borderRadius:'8px' }}
>
<DownloadIcon style={{ marginRight: '8px' }} /> {t('inventory:createpdf.download')}
</button>
</div>
);
};
export default QRCodeGenerator;
나중에 다른 코드에서 불러와서 쓰고 싶다면 이렇게 해주면 된다.
<QRCodeGenerator url={QRurl}/>
'업무 회고' 카테고리의 다른 글
keyframes로 자동으로 움직이는 Carousel 만드는 법 (0) | 2025.03.02 |
---|---|
순수 CSS로만 Sticky Scroll Cards 만들기 (0) | 2025.03.01 |
2025년 1월 개발 회고 (0) | 2025.02.18 |
2024년 11월~12월 개발 회고 (0) | 2025.01.07 |
2024년 9월~10월 개발 회고 (1) | 2024.10.31 |
댓글