qrcode.react - React QR Code Component
qrcode.react is a dedicated React component designed for generating QR codes, providing flexible rendering options through either SVG or Canvas. The package is actively maintained, with the current stable version being 4.2.0. It receives regular updates, as evidenced by recent additions like React v19 peer dependency support and new features such as `boostLevel` and the ability to encode multiple segments via an array `value` prop. A key differentiator is its extensive prop API, which allows for fine-grained control over QR code generation, including customization of size, error correction level, foreground/background colors, precise margins (`marginSize`), embedded images with opacity, and minimum QR code version. It ships with embedded TypeScript definitions, ensuring a robust development experience within modern React applications.
Common errors
-
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
cause Attempting to import `QRCodeSVG` or `QRCodeCanvas` as a default export, or using `require()` incorrectly, especially after v3.0.0 which heavily promoted named exports.fixEnsure you are using named imports: `import { QRCodeSVG } from 'qrcode.react';` instead of `import QRCodeSVG from 'qrcode.react';`. -
Property 'includeMargin' is deprecated. (property) includeMargin?: boolean
cause Using the `includeMargin` prop which has been deprecated since v4.0.0.fixReplace `includeMargin` with `marginSize`. For example, `includeMargin={true}` becomes `marginSize={4}`. -
TS2345: Argument of type 'null' is not assignable to parameter of type 'string | string[] | undefined'.
cause Passing `null` to an optional prop (like `value` if it were optional, or other optional props) after v3.1.0 changed how optional props are handled internally, potentially causing TypeScript errors.fixInstead of passing `null` to an optional prop, omit the prop entirely, or pass `undefined` if you need to explicitly clear a previously set value. -
TypeError: Cannot read properties of undefined (reading 'call') at Object.QRCodeCanvas (webpack-internal://...)
cause This error can occur in some CommonJS environments or older bundler setups when trying to access `QRCodeCanvas` or `QRCodeSVG` as properties of `require('qrcode.react')` after v3.0.0 improved ESM/CJS dual-publishing but might still cause incompatibilities.fixIf possible, switch to native ES module `import { QRCodeCanvas } from 'qrcode.react';`. If stuck with CommonJS, ensure your bundler supports dual-package exports, or try `const { QRCodeCanvas } = require('qrcode.react');`.
Warnings
- breaking Support for React versions older than 16.8 was removed in v3.0.0. Ensure your project's React dependency meets the minimum `^16.8.0` requirement.
- deprecated The default export from 'qrcode.react' was deprecated in v3.0.0 and marked with `@deprecated` JSDoc in v3.2.0. While still available, it's recommended to migrate to named exports (`QRCodeSVG` or `QRCodeCanvas`) as the default export may be removed in a future major version.
- deprecated The `includeMargin` prop was deprecated in v4.0.0 in favor of the more flexible `marginSize` prop. Using `includeMargin` will still work but might trigger deprecation warnings.
- gotcha The behavior of passing `null` to optional props changed in v3.1.0. Previously, `null` might have been treated differently due to the removal of `defaultProps` in favor of making props truly optional. This can lead to TypeScript errors if `null` was explicitly passed where `undefined` is expected for an optional prop.
- gotcha TypeScript type loading issues were present in v4.0.0, specifically when using `"moduleResolution": "Bundler"` or `"module": "ESNext"` in `tsconfig.json`. These were addressed in v4.0.1.
Install
-
npm install qrcode.react -
yarn add qrcode.react -
pnpm add qrcode.react
Imports
- QRCodeSVG
const QRCodeSVG = require('qrcode.react').QRCodeSVG;import { QRCodeSVG } from 'qrcode.react'; - QRCodeCanvas
const QRCodeCanvas = require('qrcode.react').QRCodeCanvas;import { QRCodeCanvas } from 'qrcode.react'; - Default Export (deprecated)
const QRCode = require('qrcode.react');import QRCode from 'qrcode.react'; // Avoid using
Quickstart
import ReactDOM from 'react-dom/client';
import React from 'react';
import { QRCodeSVG } from 'qrcode.react';
const App = () => (
<div style={{ padding: '20px', backgroundColor: '#f0f0f0', display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
<h1>Visit React Documentation</h1>
<p>Scan the QR code below to navigate to the official React documentation website.</p>
<QRCodeSVG
value="https://react.dev/"
size={256}
level="H" /* High error correction */
bgColor="#ffffff"
fgColor="#20232a"
marginSize={6}
imageSettings={{
src: "https://react.dev/favicon.ico",
x: undefined,
y: undefined,
height: 30,
width: 30,
excavate: true,
opacity: 0.9
}}
title="React Documentation QR Code"
minVersion={4}
boostLevel={false}
/>
<p style={{ marginTop: '20px', maxWidth: '400px', textAlign: 'center' }}>
This example demonstrates generating an SVG QR code with various customizations:
setting a specific value, size, error correction level (High), custom colors,
a larger margin, and embedding the React favicon with partial opacity.
</p>
</div>
);
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(<App />);