React QR Code Component

2.0.18 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to render a basic, responsive QR code component in a React application. It includes a user input field to dynamically update the QR code's content and applies essential styling to ensure a proper 'quiet zone' for scannability.

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>`.");
}

view raw JSON →