Skip to content

@qrcodesdk/react

React components for rendering QR codes as inline SVG, PNG, WebP, GIF, and JPEG Image elements, and Canvas elements.

Terminal window
npm install @qrcodesdk/react

Import the components directly where you need them:

import {QRCodeCanvas, QRCodePNG, QRCodeSVG} from '@qrcodesdk/react';
export function App() {
return (
<>
<QRCodeSVG payload="https://qrcodesdk.dev" />
<QRCodePNG payload="https://qrcodesdk.dev" />
<QRCodeCanvas payload="https://qrcodesdk.dev" />
</>
);
}
@qrcodesdk/react React
0.x.x ^18.0.0 ^19.0.0
ComponentOutputDownload support
QRCodeSVGInline SVG elementSVG
QRCodePNGPNG-backed <img>PNG
QRCodeWebPWebP-backed <img>WebP
QRCodeGIFGIF-backed <img>GIF
QRCodeJPEGJPEG-backed <img>JPEG
QRCodeCanvas<canvas> elementNone

Options

PropTypeDescription
payloadstring | numberRequired QR code payload.
optionsComponent-specific optionsOptional matrix and renderer options.

Each image format has its own component, so your bundle only includes the encoders of the components you import. QRCodePNG uses the browser’s native PNG encoder. QRCodeWebP renders lossless WebP, usually the smallest file, with an encoder bundled in the SDK that also works in Safari. QRCodeGIF is limited to 256 colors and binary transparency, so prefer PNG or WebP for transparent backgrounds and photographic center images. QRCodeJPEG is lossy and has no transparency: it blends transparent pixels onto a white matte by default, and quality (1 to 100, default 92) trades file size for detail. Use it when a destination requires JPEG. See the Image element renderers reference for details.

React passes className, style, and other fallthrough attributes to the component’s wrapper <div>. Use a React ref when you need a download handle; the rendered SVG, Image, or Canvas element remains inside that wrapper.

QR code for qrcodesdk.dev
import {useMemo} from 'react';
import {QRCodeSVG, type QRCodeSVGOptions} from '@qrcodesdk/react';
export default function QRCodeSVGExample() {
const options = useMemo<QRCodeSVGOptions>(
() => ({
accessibility: {
title: 'QR code for qrcodesdk.dev',
ariaLabel: 'Scan to open qrcodesdk.dev',
},
}),
[],
);
return <QRCodeSVG payload="https://qrcodesdk.dev" options={options} />;
}
import {useMemo} from 'react';
import {QRCodePNG, type QRCodePNGImageOptions} from '@qrcodesdk/react';
export default function QRCodePNGExample() {
const options = useMemo<QRCodePNGImageOptions>(
() => ({
style: {moduleSize: 8, quietZone: 4},
accessibility: {
alt: 'QR code for qrcodesdk.dev',
ariaLabel: 'Scan to open qrcodesdk.dev',
},
}),
[],
);
return <QRCodePNG payload="https://qrcodesdk.dev" options={options} />;
}
import {useMemo} from 'react';
import {QRCodeWebP, type QRCodeWebPImageOptions} from '@qrcodesdk/react';
export default function QRCodeWebPExample() {
const options = useMemo<QRCodeWebPImageOptions>(
() => ({
style: {moduleSize: 8, quietZone: 4},
accessibility: {
alt: 'QR code for qrcodesdk.dev',
ariaLabel: 'Scan to open qrcodesdk.dev',
},
}),
[],
);
return <QRCodeWebP payload="https://qrcodesdk.dev" options={options} />;
}
import {useMemo} from 'react';
import {QRCodeGIF, type QRCodeGIFImageOptions} from '@qrcodesdk/react';
export default function QRCodeGIFExample() {
const options = useMemo<QRCodeGIFImageOptions>(
() => ({
style: {moduleSize: 8, quietZone: 4},
accessibility: {
alt: 'QR code for qrcodesdk.dev',
ariaLabel: 'Scan to open qrcodesdk.dev',
},
}),
[],
);
return <QRCodeGIF payload="https://qrcodesdk.dev" options={options} />;
}
import {useMemo} from 'react';
import {QRCodeJPEG, type QRCodeJPEGImageOptions} from '@qrcodesdk/react';
export default function QRCodeJPEGExample() {
const options = useMemo<QRCodeJPEGImageOptions>(
() => ({
style: {moduleSize: 8, quietZone: 4},
quality: 90,
accessibility: {
alt: 'QR code for qrcodesdk.dev',
ariaLabel: 'Scan to open qrcodesdk.dev',
},
}),
[],
);
return <QRCodeJPEG payload="https://qrcodesdk.dev" options={options} />;
}
import {useMemo} from 'react';
import {QRCodeCanvas, type QRCodeCanvasOptions} from '@qrcodesdk/react';
export default function QRCodeCanvasExample() {
const options = useMemo<QRCodeCanvasOptions>(
() => ({
style: {
moduleSize: 8,
quietZone: 4,
foreground: '#111827',
background: '#ffffff',
},
}),
[],
);
return <QRCodeCanvas payload="https://qrcodesdk.dev" options={options} />;
}
import {useMemo, useRef} from 'react';
import {QRCodePNG, type QRCodeDownloadHandle, type QRCodePNGImageOptions} from '@qrcodesdk/react';
export default function QRCodeDownloadPNGExample() {
const qrcode = useRef<QRCodeDownloadHandle>(null);
const options = useMemo<QRCodePNGImageOptions>(
() => ({accessibility: {alt: 'QR code for qrcodesdk.dev'}}),
[],
);
return (
<div className="flex flex-col items-center gap-2">
<QRCodePNG payload="https://qrcodesdk.dev" options={options} ref={qrcode} />
<button
className="btn-primary"
onClick={() => qrcode.current?.download('qrcodesdk')}
type="button">
Download PNG
</button>
</div>
);
}

Load and decode a browser image first, then store options with centerImage: canvasCenterImage(image) in state. Updating that state rerenders the component with the prepared image. QRCodeSVG takes svgCenterImage() with an embedded data URL instead. See Add a center image for the complete React lifecycle.

QRCodeSVG, QRCodePNG, QRCodeWebP, QRCodeGIF, and QRCodeJPEG expose download(filename?) for SVG, PNG, WebP, GIF, and JPEG files respectively. The method appends the matching file extension when needed. QRCodeCanvas has no download method.

See Download or save for the complete React example, or its Canvas export section for manual downloads.

QRCodeSVG produces runtime-neutral SVG and can render on the server.

QRCodePNG, QRCodeWebP, QRCodeGIF, QRCodeJPEG, and QRCodeCanvas rely on browser DOM and Canvas APIs, so they skip element creation and downloads outside the browser and populate their host after hydration.

The package entry declares 'use client', so Server Components can import its components directly. Props crossing that boundary must be serializable. Shape path objects from @qrcodesdk/core/style can cross it, but finder(), alignment(), and center-image factory values cannot. Create those values inside a client component.

On the server, every export of a 'use client' module is a client reference that cannot be called. A Server Component must therefore import shape factories such as rounded() from @qrcodesdk/core/style, not from @qrcodesdk/react:

import {rounded} from '@qrcodesdk/core/style';
import {QRCodeSVG} from '@qrcodesdk/react';
export default function Page() {
return <QRCodeSVG payload="https://qrcodesdk.dev" options={{style: {modules: rounded()}}} />;
}

Client components can import every factory from @qrcodesdk/react:

'use client';
import {QRCodeSVG, circle, finder} from '@qrcodesdk/react';
const options = {style: {finder: finder({center: circle()})}};
export function StyledQRCode() {
return <QRCodeSVG payload="https://qrcodesdk.dev" options={options} />;
}

The options prop groups encoding settings under matrix. Renderer settings such as style, accessibility, and centerImage go alongside it:

import {type QRCodeSVGOptions, circle, finder, rounded} from '@qrcodesdk/react';
const options: QRCodeSVGOptions = {
matrix: {errorCorrectionLevel: 'H'},
style: {moduleSize: 8, quietZone: 4, modules: rounded(), finder: finder({center: circle()})},
accessibility: {ariaLabel: 'Scan to open qrcodesdk.dev'},
};

Pass this object through the component’s options prop. Use QRCodePNGImageOptions, QRCodeWebPImageOptions, QRCodeGIFImageOptions, QRCodeJPEGImageOptions, or QRCodeCanvasOptions for the corresponding component. QRCodeGIFImageOptions also accepts transparency.threshold, and QRCodeJPEGImageOptions accepts quality and transparency.matte.

Import shape and pattern factories, and center-image factories from @qrcodesdk/react alongside the components.

Mixed-mode segmentation is opt-in. Import automaticSegmenter from @qrcodesdk/react and pass it as matrix.segmenter, without installing @qrcodesdk/core separately:

import {type QRCodeSVGOptions, automaticSegmenter} from '@qrcodesdk/react';
const options: QRCodeSVGOptions = {matrix: {mode: 'auto', segmenter: automaticSegmenter}};

See Builder and matrix for the fields inside matrix, Customize appearance for style, and Renderer outputs for output-specific options.

import {
type QRCodeAlignmentCenterShape,
type QRCodeAlignmentOptions,
type QRCodeAlignmentRingShape,
type QRCodeAlignmentStyle,
type QRCodeBaseProps,
QRCodeCanvas,
type QRCodeCanvasAccessibilityOptions,
type QRCodeCanvasCenterImage,
type QRCodeCanvasOptions,
type QRCodeCanvasProps,
type QRCodeCenterImageLayout,
type QRCodeCircleShapeOptions,
type QRCodeColor,
type QRCodeDataImageURL,
type QRCodeDownloadHandle,
type QRCodeEncodingMode,
type QRCodeErrorCorrectionLevel,
type QRCodeFinderCenterShape,
type QRCodeFinderOptions,
type QRCodeFinderRingShape,
type QRCodeFinderStyle,
QRCodeGIF,
type QRCodeGIFImageOptions,
type QRCodeGIFProps,
type QRCodeImageAccessibilityOptions,
QRCodeJPEG,
type QRCodeJPEGImageOptions,
type QRCodeJPEGProps,
type QRCodeMask,
type QRCodeMatrixOptions,
type QRCodeMode,
type QRCodeModulePattern,
type QRCodeModuleShape,
QRCodePNG,
type QRCodePNGImageOptions,
type QRCodePNGProps,
type QRCodePayload,
type QRCodeRoundedShapeOptions,
QRCodeSVG,
type QRCodeSVGAccessibilityOptions,
type QRCodeSVGCenterImage,
type QRCodeSVGOptions,
type QRCodeSVGProps,
type QRCodeSegment,
type QRCodeSegmenter,
type QRCodeSegmenterOptions,
type QRCodeShape,
type QRCodeShapePath,
type QRCodeShapePiece,
type QRCodeVersion,
type QRCodeVisualStyle,
QRCodeWebP,
type QRCodeWebPImageOptions,
type QRCodeWebPProps,
alignment,
automaticSegmenter,
canvasCenterImage,
circle,
customShape,
diagonal,
finder,
rounded,
svgCenterImage,
} from '@qrcodesdk/react';

@qrcodesdk/react

Version
v0.0.5
Downloads
310/mo
Install size
485.1 kB
Bundled size
18.3 kB
Works with
Works with Google ChromeWorks with FirefoxWorks with Safari

Support QRCodeSDK

If you find QRCodeSDK useful, please consider sponsoring the project!