I try to create a qrcode app with react. After the user submit form with value to create a svg , they can also download it. But i cant make it work , each time it download the same svg.
my download function
const downloadQRCode = (e) => {
e.preventDefault();
let svg = qrRef.current.querySelector("svg");
let svgXML = new XMLSerializer().serializeToString(svg);
let dataUrl = "data:image/svg," + encodeURIComponent(svgXML);
let anchor = document.createElement("a");
anchor.href = dataUrl;
anchor.download = `qr-code.svg`;
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
};
Here i use map to render qrcode component and a button to download the svg
qrcode.map((item) => {
return (
<li ref={qrRef}>
<QRCode
className="qrcode"
size={item.size}
value={item.value}
bgColor={item.bgcolor}
fgColor={item.fgcolor}
level="L"
renderAs="svg">
</QRCode>
<form onSubmit={downloadQRCode}>
<button type="submit">Download</button>
</form>
</li>
)
}