Expensify / react-native-qrcode-svg

A QR Code generator for React Native based on react-native-svg and node-qrcode.
MIT License
1.07k stars 217 forks source link

how to auto size #181

Open sweetyguazi opened 1 year ago

sweetyguazi commented 1 year ago

I want the QR code size to automatically change with parameters such as content, error correction level, and code width, rather than fixed size. What should I do?

snoodleboot commented 1 year ago

I have the same exact use case here. Specifically these situations

  1. SafeArea
  2. When a bounding container has Max Width expand to that size
  3. Reactively shring when screen shrinks - preferrably to a min width as well
sweetyguazi commented 1 year ago

I have the same exact use case here. Specifically these situations

  1. SafeArea
  2. When a bounding container has Max Width expand to that size
  3. Reactively shring when screen shrinks - preferrably to a min width as well

I don't understand what you're saying. What I mean is that I want the size of the QR code to automatically increase as the content increases, but the size of the QR code generated by this component is fixed

jefawks3 commented 1 year ago

@sweetyguazi I had the same issue. I ended up not using this package and used the SvgXml component from the react-native-svg package directly with the output from the qrcode toString method.

For instance:

import {useEffect, useState} from 'react';
import {SvgXml} from 'react-native-svg';
import QRCode from 'qrcode';

export default ({data}) => {
  const [svgXml, setSvgXml] = useState();

  useEffect(() => {
    QRCode.toString(data, {type: 'svg' /* other options */ })
      .then(setSvgXml)
      .catch(error => {
        console.error('Unable to render SVG Code', error);
        // Do something with the error
      });
  }, [data]);

  return svgXml ? <SvgXml xml={svgXml} width="100%" height="100%" /> : null /* ActivityIndicator? */;
}

The only downside is that you'll have to handle the logo yourself. I didn't need the logo yet, so I haven't tried implementing this myself. However, if I had to take a stab at implementing it, this is what I would do: I would nest the QRCode SVG inside a root SVG with a predefined view box and nest the logo inside the root, centering the logo to the root SVG and not the QRCode SVG.

Hope this helps.

vlack-coder commented 1 year ago

@jefawks3 , this gave it an extra padding....🤷🏽‍♀️