Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
qrcode.react
Advanced tools
Package description
The qrcode.react npm package is a React component that uses the 'qrcode-generator' library to render QR codes as SVGs in React applications. It allows for easy creation and customization of QR codes directly within React components.
Generating a basic QR code
This feature allows you to generate a simple QR code by passing a string as the 'value' prop to the QRCode component.
{"import QRCode from 'qrcode.react';\n\nconst MyComponent = () => (\n <QRCode value='http://facebook.github.io/react/' />\n);"}
Customizing the QR code
This feature enables customization of the QR code by setting props such as 'size' for dimension, 'bgColor' for background color, 'fgColor' for foreground color, and 'level' for error correction level.
{"import QRCode from 'qrcode.react';\n\nconst MyComponent = () => (\n <QRCode\n value='http://facebook.github.io/react/'\n size={256}\n bgColor={'#ffffff'}\n fgColor={'#000000'}\n level={'H'}\n />\n);"}
Including a logo or image in the center of the QR code
This feature allows for the inclusion of a logo or image in the center of the QR code. The 'imageSettings' prop can be used to provide the image source and other related settings.
{"import QRCode from 'qrcode.react';\n\nconst MyComponent = () => (\n <QRCode\n value='http://facebook.github.io/react/'\n size={256}\n bgColor={'#ffffff'}\n fgColor={'#000000'}\n level={'H'}\n includeMargin={true}\n imageSettings={{\n src: 'path/to/logo.png',\n x: null,\n y: null,\n height: 24,\n width: 24,\n excavate: true,\n }}\n />\n);"}
The 'qrcode' package is a more general QR code generator that can be used in both browser and server environments. It provides a wider range of options for generating QR codes, including output formats like PNG, UTF8, and others. Unlike qrcode.react, it is not specifically tailored for React and does not render as a React component.
The 'react-qr-code' package is another React component for generating QR codes. It is similar to qrcode.react in that it is designed for React and renders QR codes as SVGs. However, it may have different customization options and dependencies.
The 'react-native-qrcode-svg' package is designed for React Native applications and allows for the generation of QR codes as SVG elements. It is similar to qrcode.react but is specifically built for the React Native environment rather than for web applications.
Changelog
[3.1.0] - 2022-06-25
defaultProps
. This may be a slight behavior change with TypeScript if previously passing null
.shapeRendering="crispEdges"
in SVG component.Readme
A React component to generate QR codes for rendering to the DOM.
npm install qrcode.react
qrcode.react
exports three components, supporting rendering as SVG or Canvas. SVG is generally recommended as it is more flexible, but Canvas may be preferable.
All examples are shown using modern JavaScript modules and syntax. CommonJS require('qrcode.react')
is also supported.
QRCodeSVG
import ReactDOM from 'react-dom';
import {QRCodeSVG} from 'qrcode.react';
ReactDOM.render(
<QRCodeSVG value="https://reactjs.org/" />,
document.getElementById('mountNode')
);
QRCodeCanvas
import ReactDOM from 'react-dom';
import {QRCodeCanvas} from 'qrcode.react';
ReactDOM.render(
<QRCodeCanvas value="https://reactjs.org/" />,
document.getElementById('mountNode')
);
QRCode
- DEPRECATEDNote: Usage of this is deprecated as of v3. It is available as the default
export for compatiblity with previous versions. The renderAs
prop is only supported with this component.
import ReactDOM from 'react-dom';
import QRCode from 'qrcode.react';
ReactDOM.render(
<QRCode value="https://reactjs.org/" renderAs="canvas" />,
document.getElementById('mountNode')
);
prop | type | default value |
---|---|---|
value | string | |
renderAs | string ('canvas' 'svg' ) | 'canvas' |
size | number | 128 |
bgColor | string (CSS color) | "#FFFFFF" |
fgColor | string (CSS color) | "#000000" |
level | string ('L' 'M' 'Q' 'H' ) | 'L' |
includeMargin | boolean | false |
imageSettings | object (see below) |
imageSettings
field | type | default value |
---|---|---|
src | string | |
x | number | none, will center |
y | number | none, will center |
height | number | 10% of size |
width | number | 10% of size |
excavate | boolean | false |
qrcode.react
will pass through any additional props to the underlying DOM node (<svg>
or <canvas>
). This allows the use of inline style
or custom className
to customize the rendering. One common use would be to support a responsive layout.
Note: In order to render QR Codes in <canvas>
on high density displays, we scale the canvas element to contain an appropriate number of pixels and then use inline styles to scale back down. We will merge any additional styles, with custom height
and width
overriding our own values. This allows scaling to percentages but if scaling beyond the size
, you will encounter blurry images. I recommend detecting resizes with something like react-measure to detect and pass the appropriate size when rendering to <canvas>
.
qrcode.react
supports encoding text only, in a single segment. The encoding library being used does minimal detection to determine if the text being encoded can follow an optimized path for Numeric or Alphanumeric modes, allowing for more data to be encoded. Otherwise, it will encode following Byte mode. This mode includes supports multi-byte Unicode characters such as Kanji, however it does not support the optimized Kanji encoding mode.
qrcode.react
is licensed under the ISC license.
qrcode.react
bundles QR Code Generator, which is available under the MIT license.
FAQs
React component to generate QR codes
The npm package qrcode.react receives a total of 1,200,887 weekly downloads. As such, qrcode.react popularity was classified as popular.
We found that qrcode.react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.