React components for generating customizable QR codes with SVG and Canvas rendering options
npx @tessl/cli install tessl/npm-qrcode-react@4.2.0QR Code React is a React component library for generating customizable QR codes. It provides two rendering options (SVG and Canvas) with comprehensive customization including colors, sizes, error correction levels, margins, and embedded image support.
npm install qrcode.reactimport { QRCodeSVG, QRCodeCanvas } from "qrcode.react";For CommonJS:
const { QRCodeSVG, QRCodeCanvas } = require("qrcode.react");import React from "react";
import { QRCodeSVG, QRCodeCanvas } from "qrcode.react";
// Basic SVG QR code
function BasicSVGExample() {
return <QRCodeSVG value="https://reactjs.org/" />;
}
// Basic Canvas QR code
function BasicCanvasExample() {
return <QRCodeCanvas value="https://reactjs.org/" />;
}
// Customized QR code with image
function CustomizedExample() {
return (
<QRCodeSVG
value="https://example.com"
size={256}
bgColor="#ffffff"
fgColor="#000000"
level="M"
marginSize={4}
imageSettings={{
src: "https://example.com/logo.png",
height: 32,
width: 32,
excavate: true
}}
/>
);
}QR Code React is built around several key components:
SVG-based QR code component providing scalable vector rendering with full customization options.
const QRCodeSVG: React.ForwardRefExoticComponent<
QRPropsSVG & React.RefAttributes<SVGSVGElement>
>;Canvas-based QR code component optimized for high-density displays with pixel-perfect rendering.
const QRCodeCanvas: React.ForwardRefExoticComponent<
QRPropsCanvas & React.RefAttributes<HTMLCanvasElement>
>;Base props interface shared by both QR code components.
interface QRProps {
/** The value to encode into the QR Code. Array of strings for optimized multi-segment encoding */
value: string | string[];
/** Size in pixels to render the QR Code (default: 128) */
size?: number;
/** Error correction level (default: 'L') */
level?: ErrorCorrectionLevel;
/** Background color for the QR Code (default: '#FFFFFF') */
bgColor?: string;
/** Foreground color for the QR Code (default: '#000000') */
fgColor?: string;
/** @deprecated Use marginSize instead. Whether to include 4-module margin (default: false) */
includeMargin?: boolean;
/** Number of modules for margin, overrides includeMargin (default: 0) */
marginSize?: number;
/** Title for accessibility */
title?: string;
/** Minimum QR version 1-40, higher = more complex (default: 1) */
minVersion?: number;
/** Allow higher error correction if possible without increasing version (default: true) */
boostLevel?: boolean;
/** Settings for embedded image/logo */
imageSettings?: ImageSettings;
}QR code error correction level options.
type ErrorCorrectionLevel = 'L' | 'M' | 'Q' | 'H';'L' - Low (~7% error correction)'M' - Medium (~15% error correction)'Q' - Quartile (~25% error correction)'H' - High (~30% error correction)Configuration for embedding images or logos in QR codes.
interface ImageSettings {
/** URI of the embedded image */
src: string;
/** Height of the image in pixels */
height: number;
/** Width of the image in pixels */
width: number;
/** Whether to excavate (clear) modules behind the image */
excavate: boolean;
/** Horizontal offset in pixels, centers if not specified */
x?: number;
/** Vertical offset in pixels, centers if not specified */
y?: number;
/** Image opacity 0-1 (default: 1) */
opacity?: number;
/** Cross-origin attribute for image loading */
crossOrigin?: CrossOrigin;
}Cross-origin attribute values for image loading in QR codes.
type CrossOrigin = 'anonymous' | 'use-credentials' | '' | undefined;Props types for individual QR code components that extend base QRProps with DOM attributes.
type QRPropsCanvas = QRProps & React.CanvasHTMLAttributes<HTMLCanvasElement>;
type QRPropsSVG = QRProps & React.SVGAttributes<SVGSVGElement>;Optimize QR code size by using multiple segments:
import { QRCodeSVG } from "qrcode.react";
function MultiSegmentExample() {
return (
<QRCodeSVG
value={["WIFI:", "S:MyNetwork;", "T:WPA;", "P:password123;", ";"]}
size={200}
level="M"
/>
);
}Apply custom styles using className and style props:
import { QRCodeSVG } from "qrcode.react";
function StyledExample() {
return (
<QRCodeSVG
value="https://example.com"
size={150}
className="qr-code"
style={{
border: "2px solid #333",
borderRadius: "8px",
padding: "16px"
}}
/>
);
}Use higher error correction when embedding logos:
import { QRCodeCanvas } from "qrcode.react";
function LogoExample() {
return (
<QRCodeCanvas
value="https://company.com"
size={300}
level="H" // High error correction for logo overlay
bgColor="#f8f9fa"
fgColor="#212529"
marginSize={4}
imageSettings={{
src: "/company-logo.png",
height: 60,
width: 60,
excavate: true,
opacity: 0.9,
crossOrigin: "anonymous"
}}
/>
);
}Access canvas element for data extraction:
import React, { useRef, useCallback } from "react";
import { QRCodeCanvas } from "qrcode.react";
function CanvasRefExample() {
const canvasRef = useRef<HTMLCanvasElement>(null);
const downloadQRCode = useCallback(() => {
if (canvasRef.current) {
const url = canvasRef.current.toDataURL();
const link = document.createElement('a');
link.download = 'qrcode.png';
link.href = url;
link.click();
}
}, []);
return (
<div>
<QRCodeCanvas
ref={canvasRef}
value="https://example.com"
size={256}
/>
<button onClick={downloadQRCode}>Download QR Code</button>
</div>
);
}Create responsive QR codes that scale with container:
import React, { useState, useEffect } from "react";
import { QRCodeSVG } from "qrcode.react";
function ResponsiveExample() {
const [size, setSize] = useState(200);
useEffect(() => {
const handleResize = () => {
const container = document.getElementById('qr-container');
if (container) {
const containerWidth = container.offsetWidth;
setSize(Math.min(containerWidth - 32, 400)); // Max 400px with 16px padding
}
};
window.addEventListener('resize', handleResize);
handleResize(); // Initial size
return () => window.removeEventListener('resize', handleResize);
}, []);
return (
<div id="qr-container" style={{ width: '100%', padding: '16px' }}>
<QRCodeSVG
value="https://example.com"
size={size}
style={{ maxWidth: '100%', height: 'auto' }}
/>
</div>
);
}