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

198
Visualizações
Share QR React Native

I'm new in react/react native. I'm trying to share a QR Code as image. Generate QR works, but I want to share it as an image (whatsapp, bluetooth, etc).

import QRCode from 'react-native-qrcode-svg';
let svg = useRef();
//let svg = '';

<QRCode
        size={300}
        value={`${name}`}
        getRef={(c) => (svg = c)}
/>

I tried "get base64 string encode of the qrcode" from official documentation, but I just don't get it

//From Off Doc
getDataURL() {
 this.svg.toDataURL(this.callback);
}

callback(dataURL) {
  console.log(dataURL);
}

What I tried to do (all my code):

import React, { useRef } from 'react';
import QRCode from 'react-native-qrcode-svg';
const QR = ({ name }: any) => {
   let svg = useRef();

 const getDataURL = () => {
   svg.toDataURL(callback(dataURL));
   //console.log(svg);
 }


 callback(dataURL) {
   console.log(dataURL);
 }

return (
    <>
        <QRCode
            size={300}
            value={`${name}`}
            getRef={(c) => (svg = c)}
        />

        
            
        <Button onPress={getDataURL}
            title="Call Funct"
            color="#1FAAE2" />
        
    </>
    
);

get error svg.toDataURL is not a function. I have been in this for days, I also read another stackover queries with the same problem but solutions in those questions didn't work for me. Thank you in advance guys

Error toDataURL

console.log(svg)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I have changed a couple of things in your code and used it on a expo app where I installed react-native-qrcode-svg and react-native-svg

import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View, TextInput, Button } from "react-native";
import { useRef } from "react";
import QRCode from "react-native-qrcode-svg";

const QR = ({ name }: any) => {
  let svg = useRef<SVG>(null);

  const getDataURL = () => {
    svg?.toDataURL(callback);
    //console.log(svg);
  };

  function callback(dataURL: string) {
    console.log(dataURL);
  }

  return (
    <>
      <QRCode size={300} value={`${name}`} getRef={(c) => (svg = c)} />

      <Button onPress={getDataURL} title="Call Funct" color="#1FAAE2" />
    </>
  );
};
export default function App() {
  const input = useRef<TextInput>(null);

  return (
    <View style={styles.container}>
      <Text>Open up App.tsx to start working on your app!</Text>
      <StatusBar style="auto" />
      <QR />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
});

Main changes from your code is defining the callback as a function

// you had 
 callback(dataURL) {
   console.log(dataURL);
 }
// but it should be
 function callback(dataURL) {
   console.log(dataURL);
 }
// or 
const callback = (dataURL) => {
   console.log(dataURL)
}

and doing the call properly on getDataURL

// you had

   svg.toDataURL(callback(dataURL));

// but it should be
   svg.toDataURL(callback);


After those changes clicking in the button returns the dataURL in the console as expected.


Old answer before question edit:

Your issue seems to be that svg is not defined when you call svg.toDataURL how are you calling the function? If you are doing that on the first render it is possible that the ref is not ready yet.

If you are doing that using a callback in a button in the screen then the issue should be around the code setting the ref.

Can you post your whole component?

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