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

getRef={ c =>{ c.toDataURL // undefiend } how to shear QrCode as image #148

Open
SiddharthAlagesan opened this issue Aug 18, 2021 · 3 comments

Comments

@SiddharthAlagesan
Copy link

_this.svg.toDataURL is not a function

15 |
16 | saveQRCode = () => {

17 | this.svg.toDataURL(this.callback);
| ^ 18 | }


  saveQRCode = () => {
    this.svg.toDataURL(this.callback);
  };

  callback(dataURL) {
    console.log(dataURL);
    let shareImageBase64 = {
      title: 'Required Data ',
      url: `data:image/png;base64,${dataURL}`,
      subject: 'Share Link', //  for email
    };
    console.log(dataURL)
    Share.open(shareImageBase64).catch(error => console.log(error));
  }
<TouchableOpacity onPress={this.shareQR}>
          <QRCode
            value={this.state.input }
            size={250} 
            color="#000"
            getRef={ c =>{  
                              this.svg= c;
                             console.log(c)   //Svg {props: {…}, context: {…}, refs: {…}, updater: {…}, _remeasureMetricsOnActivation: ƒ, …}
                             console.log(c.toDataURL )  // undefined
                             }}/>
</TouchableOpacity>
 <Button onPress={this.saveQRCode} title="Shear"  />
 />
@SiddharthAlagesan SiddharthAlagesan changed the title how to shear QrCode as image getRef={ c =>{ c.toDataURL // undefiend } how to shear QrCode as image Aug 18, 2021
@schmidi64
Copy link

schmidi64 commented Aug 22, 2021

I fixed this problem by using react-native-view-shot.
I use expo-captureRef in the example.

From "captureRef" you will get the path to the screenshot. For example you can share the screenshot with this path.
Apply some padding to the view which is outside the qr-code element - this makes it way better to scan.
The prop "collapsable={false}" is needed to get a valid ref from the view element.
The function "someFunction" is called onClick in my case.

Something like this:

function QrCode() {
  const viewRef = useRef()
  
  const someFunction = async () => {
      const path = await captureRef(viewRef.current, {
        quality: 1,
        format: 'png',
      })
   }
  
  return (
    <View style={styles.xy} collapsable={false} ref={(ref) => viewRef.current = ref}>
              <QRCode
                  color='black'
                  backgroundColor='white'
                  value="Some String"
                  size={Dimensions.get('window').width - 40}
              />
    </View>
  )
}

It is more like a workaround but it works :D

@hunghg255
Copy link

You can assign base64 to ref by this one

const refQrCode = useRef();
  
  return <>
    <QrCode 
      getRef={c => {
        if (!c?.toDataURL) return;

        c?.toDataURL(base64Image => {
          refQrCode.current = base64Image;
        })
      }}
    />
  </>;

@narcismihaitech
Copy link

You can assign base64 to ref by this one

const refQrCode = useRef();
  
  return <>
    <QrCode 
      getRef={c => {
        if (!c?.toDataURL) return;

        c?.toDataURL(base64Image => {
          refQrCode.current = base64Image;
        })
      }}
    />
  </>;

This worked for me.

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

4 participants