Last active
December 21, 2023 10:31
-
-
Save ravjotsandhu/2830d356f4dd80cd836ed65d4b6465e4 to your computer and use it in GitHub Desktop.
QR Code Generator in React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useState, useRef} from 'react'; | |
import { | |
SafeAreaView, | |
Text, | |
View, | |
StyleSheet, | |
TextInput, | |
TouchableOpacity, | |
Share, | |
} from 'react-native'; | |
import QRCode from 'react-native-qrcode-svg'; | |
const App = () => { | |
const [qrvalue, setQrvalue] = useState([ | |
{id: 4, name: 'phone', brand: 'MI 5', price: '12000'}, | |
]); | |
let myQRCode = useRef(); | |
const shareQRCode = () => { | |
myQRCode.toDataURL(dataURL => { | |
console.log(dataURL); | |
let shareImageBase64 = { | |
title: '$', | |
url: `data:image/png;base64,${dataURL}`, | |
subject: 'Share Link', | |
}; | |
Share.share(shareImageBase64).catch(error => console.log(error)); | |
}); | |
}; | |
return ( | |
<SafeAreaView style={{flex: 1}}> | |
<View style={styles.container}> | |
<Text style={styles.titleStyle}> | |
Generation of QR Code in React Native | |
</Text> | |
<QRCode | |
// ref={myQRCode} | |
getRef={ref => (myQRCode = ref)} | |
//QR code value | |
value={JSON.stringify(...qrvalue)} | |
//size of QR Code | |
size={250} | |
//Color of the QR Code (Optional) | |
color="black" | |
//Background Color of the QR Code (Optional) | |
backgroundColor="white" | |
/> | |
<TouchableOpacity style={styles.buttonStyle} onPress={shareQRCode}> | |
<Text style={styles.buttonTextStyle}>Share QR Code</Text> | |
</TouchableOpacity> | |
</View> | |
</SafeAreaView> | |
); | |
}; | |
export default App; | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
backgroundColor: 'white', | |
justifyContent: 'center', | |
alignItems: 'center', | |
textAlign: 'center', | |
padding: 10, | |
}, | |
titleStyle: { | |
fontSize: 20, | |
textAlign: 'center', | |
margin: 10, | |
}, | |
textStyle: { | |
textAlign: 'center', | |
margin: 10, | |
}, | |
textInputStyle: { | |
flexDirection: 'row', | |
height: 40, | |
marginTop: 20, | |
marginLeft: 35, | |
marginRight: 35, | |
margin: 10, | |
}, | |
buttonStyle: { | |
backgroundColor: 'red', | |
borderWidth: 0, | |
color: 'red', | |
borderColor: '#51D8C7', | |
alignItems: 'center', | |
borderRadius: 5, | |
marginTop: 30, | |
padding: 10, | |
}, | |
buttonTextStyle: { | |
color: 'white', | |
paddingVertical: 10, | |
fontSize: 16, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment