Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

309
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda