rn-qrcode-svg
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

rn-qrcode-svg

It's a React Native component to generate QRCode. It uses SVG (react-native-svg) to draw QRCode.

How to install this package

Because this is a React Native component, you must install it on a React Native project. Beside that, you must also install react-native-svg package. You may use the following console command:

npm i react-native-svg rn-qrcode-svg

For iOS, there is another step:

cd ios && pod install && cd ..

Component Props

Name Type Description Default Value
bgColor color value The light color (canvas color) "transparent"
ecl enum("L", "M", "Q", "H") Error correction level for QR code:
  • L (low)
  • M (medium)
  • Q (quartile)
  • H (high)
You can use predefined constants as in the example below:
...
import QRCode, {ECL} from 'rn-qrcode-svg';
...
    <QRCode ecl={ECL.M}  ... />
"H"
fgColor color value The dark color (dots color) "black"
logo Object:
{
    asBackground?: boolean,
    centerized?: boolean,
    height?: number | string,
    href: ImageSource | string,
    opacity?: number | string,
    preserveAspectRatio?: string,
    width?: number | string,
    x?: number | string,
    y?: number | string,
}
It's to set the logo image that you want to place on the QRCode image. This prop value is an object containing some properties which determine the attributes of logo. The only required property is href. This property is to set the source of image. Almost all properties are the same as the props of svg Image element. Two properties which do not belong to Image element:
  • asBackground is a boolean value to determine whether the logo image is drawn behind of QRCode dots or in the front of them. The default value is false (the logo is drawn in front of the QRCode dots or it will cover some dots).
  • centerized is a boolean value to determine whether the logo is positioned at the center of QRCode image or not. The default value is false which means the position of logo is determined by the value of x and y property. If the position of logo is centerized then x and y property are ignored.
Beside the properties listed here, you may also use the common props for svg elements.

Need to know that we can place a logo on a QRCode image because of the existence of error correction in QRCode. But, we must still set the position and dimension of logo carefully because it can cause the QRCode cannot be read.
onError function(errorMessage) The handler that will be called when an error happens in generating QRCode image It will displays an alert dialog saying "Cannot generate QR Code" and logs the error message to the console.
size number The size of QRCode image 128
value All data that can be passed to qr function The encoded data Required (you must set this prop)
version number range[1..40] The size of QRCode. Different from size prop, it's the size of data matrix. There are 40 sizes available: the smallest (version 1) is 21x21 and the largest (vesrsion 40) is 177x177. There will be an error if the chosen size is too small to accommodate the encoded data. The data capacity is also affected by the error correction level (higher level is less capacity). The smallest size that can accommodate the encoded data.

Example

import React from 'react';
import {Button, SafeAreaView, StyleSheet, Text, TextInput, View} from 'react-native';
import QRCode from 'rn-qrcode-svg';

const styles = StyleSheet.create({
    line: {
        alignItems: 'center',
        flexDirection: 'row',
        marginHorizontal: 10,
        marginVertical: 5,
    },
    main: {
        backgroundColor: '#aaa',
        flex: 1,
    },
    qrBox: {
        alignItems: 'center',
        justifyContent: 'center',
        padding: 10,
    },
    text: {
        borderWidth: 1,
        flex: 1,
        paddingVertical: 0,
    },
});

export default function() {
    const [value, setValue] = React.useState("https://reactnative.dev/docs"),
          [size, setSize] = React.useState('128'),
          [bgColor, setBgColor] = React.useState('transparent'),
          [fgColor, setFgColor] = React.useState('black'),
          [qrProp, setQrProp] = React.useState({value, size, bgColor, fgColor});
    return <SafeAreaView style={styles.main}>
        <View style={styles.line}>
            <Text>{'value: '}</Text>
            <TextInput value={value} onChangeText={setValue} style={styles.text} />
        </View>
        <View style={styles.line}>
            <Text>{'size: '}</Text>
            <TextInput value={size} onChangeText={setSize} style={styles.text} />
            <Text>{' bgColor: '}</Text>
            <TextInput value={bgColor} onChangeText={setBgColor} style={styles.text} />
            <Text>{' fgColor: '}</Text>
            <TextInput value={fgColor} onChangeText={setFgColor} style={styles.text} />
        </View>
        <Button title='Update' onPress={() => setQrProp({value, size, bgColor, fgColor})} />
        <View style={styles.qrBox}>
            <QRCode {...qrProp} />
        </View>
    </SafeAreaView>;
}

Output:

Example output

<QRCode value="https://reactnative.dev/docs" logo={{href: require('./images/react-native.png')}} />

Output:

Example output

<QRCode
    value="https://reactnative.dev/docs"
    logo={{
        href: require('./images/react-native.png'),
        centerized: true,
        height: 40,
        width: 60,
    }}
/>

Output:

Example output

<QRCode
    value="https://reactnative.dev/docs"
    logo={{
        href: require('./images/react-native.png'),
        asBackground: true,
        height: '60%',
        width: '60%',
        x: '50%',
        y: '60%',
    }}
/>

Example output

Readme

Keywords

Package Sidebar

Install

npm i rn-qrcode-svg

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

112 kB

Total Files

9

Last publish

Collaborators

  • atmulyana