React QR Code Component
react-qr-code is a JavaScript library providing a `<QRCode />` component for generating and rendering QR codes within both React and React Native applications. As of version 2.0.18, the library is actively maintained with frequent patch releases, ensuring compatibility and addressing minor bugs. It differentiates itself by offering a consistent API for both web and mobile platforms, leveraging `react-native-svg` for its React Native implementation. The component strictly adheres to the official QR code specification, supporting various error correction levels and substantial data capacities, making it a reliable choice for easily integrating QR code generation into the React ecosystem.
Common errors
-
Invariant Violation: View config not found for name 'RNSVGPath'. Are you sure this native component exists and was properly linked?
cause The `react-native-svg` library, which is essential for rendering QR codes in React Native, is not installed or linked correctly.fixInstall `react-native-svg` via `npm install react-native-svg` (or `yarn add react-native-svg`) and then navigate to your `ios` directory and run `pod install`. -
TypeError: (0, react_qr_code_1.default) is not a function
cause Incorrect import statement. The `QRCode` component is a default export, but it's being imported as a named export.fixChange your import statement from `import { QRCode } from 'react-qr-code';` to `import QRCode from 'react-qr-code';`. -
QR code generated but is not scannable by common QR code readers.
cause Insufficient or missing 'quiet zone' (the clear space) around the QR code, or poor color contrast between foreground and background.fixAdd a white or light-colored background with adequate padding around the `<QRCode />` component (e.g., `<div style={{ background: 'white', padding: '16px' }}><QRCode ... /></div>`). Also, ensure `fgColor` and `bgColor` props provide strong contrast.
Warnings
- gotcha For React Native applications, `react-native-svg` must be installed manually. Although it was a peer dependency prior to version 2.0.15, it is now an implicit runtime requirement and will cause rendering issues if not present.
- gotcha To ensure reliable scanning, a 'quiet zone' (a clear, light-colored border) around the QR code is crucial. Without it, some scanners may struggle to read the code.
- gotcha The `QRCode` component is exported as a default export. Attempting to import it as a named export (e.g., `import { QRCode } from 'react-qr-code';`) will result in a runtime `TypeError`.
- breaking Version 2.0.16 included a bug fix to update the internal `render()` return type to `ReactNode` for compatibility with React 19. While this primarily affects internal types, it could potentially impact applications with highly customized rendering logic or strict TypeScript configurations when upgrading to React 19.
Install
-
npm install react-qr-code -
yarn add react-qr-code -
pnpm add react-qr-code
Imports
- QRCode
import { QRCode } from 'react-qr-code';import QRCode from 'react-qr-code';
- QRCode
const { QRCode } = require('react-qr-code');const QRCode = require('react-qr-code'); - QRCodeProps
import type { QRCodeProps } from 'react-qr-code';
Quickstart
import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';
import QRCode from 'react-qr-code';
function App() {
const [value, setValue] = useState('https://checklist.day');
return (
<div style={{
padding: '20px',
backgroundColor: '#f0f0f0',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
minHeight: '100vh'
}}>
<h1>QR Code Generator</h1>
<p>Enter text or a URL below to generate a QR code:</p>
<input
type="text"
value={value}
onChange={(e) => setValue(e.target.value)}
style={{ marginBottom: '20px', width: '300px', padding: '8px', fontSize: '16px', borderRadius: '4px', border: '1px solid #ccc' }}
placeholder="Enter QR code content"
/>
{value && (
<div style={{
background: 'white',
padding: '16px',
borderRadius: '8px',
boxShadow: '0 4px 8px rgba(0,0,0,0.1)',
display: 'inline-block'
}}>
<QRCode
size={256}
style={{ height: "auto", maxWidth: "100%", width: "100%" }}
value={value}
viewBox={`0 0 256 256`}
level="H"
fgColor="#333333"
bgColor="#FFFFFF"
/>
</div>
)}
{!value && <p>Please enter some content to generate a QR code.</p>}
<p style={{ marginTop: '20px', fontSize: '14px', color: '#666' }}>Scan this QR code with your device.</p>
</div>
);
}
// Assuming a root HTML element with id='root' exists
const rootElement = document.getElementById('root');
if (rootElement) {
const root = ReactDOM.createRoot(rootElement);
root.render(<App />);
} else {
console.error("No element with ID 'root' found to render the React app. Ensure your HTML has `<div id='root'></div>`.");
}