Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useRef hook problem #126

Open
RnzTejada opened this issue Nov 19, 2020 · 7 comments
Open

useRef hook problem #126

RnzTejada opened this issue Nov 19, 2020 · 7 comments

Comments

@RnzTejada
Copy link

Hi everyone, i trying to get de ref of the component with hooks and im getting null or undenifed

import React, { useState,useRef } from "react";
import Logo from "../../assets/img/logo-wollet-1.svg";
import QRCode from "qrcode.react";

export default function SelectOptions() {
  const qrRef = useRef()
  const imageSettings = {
    src: Logo,
    height: 50,
    width: 50,
    excavate: true,
  };

const getQr = ()=>{
let img = qrRef.current.toDataURL('img/png')
}

  return (
    <>
      <QRCode
        ref={qrRef}
        value={url}
        renderAs="canvas"
        size={250}
        bgColor={"transparent"}
        fgColor={"#EB346D"}
        level={"M"}
        imageSettings={imageSettings}
      />

<button onClick={getQr}>Get the qr</button>
  ...
    </>
  );
}

any suggestions to get the correct ref of canvas element ?

@beautyfree
Copy link

same issue

@xueerli
Copy link

xueerli commented May 24, 2021

+1

@markgzl
Copy link

markgzl commented Oct 22, 2021

+1

1 similar comment
@LionyxML
Copy link

+1

@yaser-alazm
Copy link

You cannot use ref on the original QRCode component, as a work around it you can wrap the QRCode component with a div element and pass the rep into the div element like so:

              <div ref={qrCodeEl}>
                <QRCode
                  value='qr-value'
                  size={175}
                />
              </div>

and then you can get the QRCode canvas element like so:
const canvas = qrCodeEl.current.children[0]

@LionyxML
Copy link

Thanks! That worked!

1 similar comment
@Glittergalaxy
Copy link

Thanks! That worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants