Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
qrcode.react
Advanced tools
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.
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.
[3.0.0] - 2022-03-26
QRCodeSVG
and QRCodeCanvas
.qr.js
dependency for vendored version of https://www.nayuki.io/page/qr-code-generator-library.FAQs
React component to generate QR codes
The npm package qrcode.react receives a total of 1,202,667 weekly downloads. As such, qrcode.react popularity was classified as popular.
We found that qrcode.react demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.