Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

304
Views
Button download always target and download the first svg in my collection , how to make it work so that is target each specific svg

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>
)
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It looks like you are using a single React ref for all the qrcode you are mapping. I suggest using an array of React refs and passing this to the button's onClick handler.

const qrcodeRefs = React.useRef([]);

qrcodeRefs.current = qrcode.map((_, i) => qrcodeRefs.current[i] ?? createRef());

...

const downloadQRCode = (qrRef) => e => {
  const svg = qrRef.current.querySelector("svg");
  const svgXML = new XMLSerializer().serializeToString(svg);
  const dataUrl = "data:image/svg," + encodeURIComponent(svgXML);

  const anchor = document.createElement("a");
  anchor.href = dataUrl;
  anchor.download = `qr-code.svg`;
  document.body.appendChild(anchor);
  anchor.click();
  document.body.removeChild(anchor);
};

...

qrcode.map((item, index) => {
  return (
    <li ref={qrcodeRefs.current[i]}>
      <QRCode
        className="qrcode"
        size={item.size}
        value={item.value}
        bgColor={item.bgcolor}
        fgColor={item.fgcolor}
        level="L"
        renderAs="svg"
      />
      <button type="button" onClick={downloadQRCode(qrcodeRefs.current[i])}>
        Download
      </button>
    </li>
  )
}
about 4 years ago · Juan Pablo Isaza Report

0

if you use ref in a loop, it will hold the last value. you do not need a ref, replace

let svg = qrRef.current.querySelector("svg");

with

// e.target.parentNode is the li dom
let svg = e.target.parentNode.querySelector("svg");

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!