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}/>'코딩' 카테고리의 다른 글
| Swiper를 사용해서, 슬라이드 만드는 법 (0) | 2025.03.03 |
|---|---|
| keyframes로 자동으로 움직이는 Carousel 만드는 법 (0) | 2025.03.02 |
| 순수 CSS로만 Sticky Scroll Cards 만들기 (0) | 2025.03.01 |
| props 상위 컴포넌트로 올리고 하위 컴포넌트로 내리기 (0) | 2024.09.26 |
| useRef 사용해서 컴포넌트 바깥을 클릭했을 때 컴포넌트가 닫히게 하기 (0) | 2024.09.26 |
댓글